public class StringCase {
public static void main(String[] args) {
//验证一
String s1 = " zhang san ";
System.out.println("练习一:");
System.out.println("("+s1+")");
System.out.println("("+myTrim(s1)+")");
}
/**
* 练习一,自定义方法去除字符串两边的空格
* @param s
* @return
*/
public static String myTrim(String s){
int start= 0,end = s.length()-1;
//判断前边的空格
while(start <= end && s.charAt(start) == ' '){
start++;
}
//判断后边的空格
while(end >= start && s.charAt(end) == ' '){
end--;
}
return s.substring(start,end+1);
}
} |
|