- public class StringTest3 {
- public static void main(String[] args){
- String s = " ab c ";
- s = myTrim(s);
- System.out.println("-"+s+"-");
- }
- public static String myTrim(String s){
- int start = 0, end = s.length()-1;
- //从头开始判断字符是否是空格
- while(start <= end && s.charAt(start) == ' '){
- start++;
- }
- //从尾开始判断字符是否是空格
- while(start <= end && s.charAt(end) == ' '){
- end--;
- }
- return s.substring(start, end+1);
- }
- }
复制代码
|
|