class StringTest {
public static void sop(String str) {
System.out.println(str);
}
public static void main(String[] args) {
String s = " ab cd ";
System.out.print("原样输出:");
sop("(" + s + ")");
System.out.print("去除前后空格:");
s = myTrim(s);
sop("(" + s + ")");
}
public static String myTrim(String str) {
int start = 0, end = str.length() - 1;
while(start<=end && str.charAt(start) == ' ') {
start++;
}
while(end>=start && str.charAt(end) == ' ') {
end--;
}
return str.substring(start, end+1);
}
}
Its_forever
相关分类