模拟一个trim方法,去除字符串两端的空格。

@Test
public void test1() {
String str1 = " h e llo wor ld ";
int begin = 0;
int end = str1.length() - 1;
while (begin <= end && str1.charAt(begin) == ' '){
begin ++;
}
while (begin <= end && str1.charAt(end) == ' '){
end --;
}
System.out.println(begin);
System.out.println(end);
String substring = str1.substring(begin, end+1);
System.out.println(substring);
}
posted @ 2021-04-12 19:03  可一可再h  阅读(579)  评论(0)    收藏  举报