public class StringTest4 {
/**
* 模拟一个与trim功能一致的方法
*/
public static void main(String[] args) {
String s=" ab c ";
String ms=myTrim(s);
System.out.println('-'+ms+'-');
}
private static String myTrim(String s) {
int start=0;
int end=s.length()-1;
while (start<=end&&s.charAt(start)==' ') {
start++;
}
while (start<=end&&s.charAt(end)==' ') {
end--;
}
return s.substring(start,end+1);
}
}
模拟一个与trim功能一致的方法
最新推荐文章于 2022-10-08 19:57:01 发布