现在的位置: 首页 > 综合 > 正文

模仿trim功能,取出两边空白

2017年12月19日 ⁄ 综合 ⁄ 共 363字 ⁄ 字号 评论关闭
public class liketrim {
	public static void main(String[] args){
		String s = "  ab  c   ";
		System.out.println("old_s:"+s);
		String ns = mytrim(s);
		System.out.println("new_s:"+ns);
		
	}
	public static String mytrim(String s){
		int a = s.length();
		int start = 0;
		int end = a-1;
		while(start<=end&&s.charAt(start)==' '){
			start++;
		}
		while(start<=end&&s.charAt(end)==' '){
			end--;
		}
		String sub = s.substring(start, end+1);
		return sub;
	}
}

抱歉!评论已关闭.