您的位置:首页 > 其它

15-12-常用对象API(String类-练习4-去除两端空白)

2015-08-10 22:57 447 查看
package cn.itcast.string.demo;

public class StringTest4 {

public static void main(String[] args) {
/*
* 4.模拟一个trim功能一致的方法
*/

String s = "        abcd        ";

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--;
}

String sub = s.substring(start, end+1);

return sub;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: