package string;
public class StringTest5 {
/*
* 字符串去掉空格事例
*/
public static void main(String[] args) {
String str = "";
System.out.println(getStringTrim(str));
}
private static String getStringTrim(String str) {
int start = 0;
int end = str.length() - 1;
while (start <= end && str.charAt(start) == ' ') {
start++;
}
while (start <= end && str.charAt(end) == ' ') {
end--;
}
return str.substring(start, end + 1);
}
}
就像这个程序,为什么 str.length() - 1 还有结尾end + 1,一些排序方法里也有好多长度-1,谁能告诉我 |
|