常用对象API(String类-去除两端空白)

模拟一个trim功能一致的方法。去除字符串两端空白

1.定义两个变量

一个变量作为从头开始判断字符串空格的角标。不断++

一个变量作为从尾开始判断字符串空格的角标。不断--

2.判断到不是空格为止,取头尾之间字符串即可。(取子串)

package stringd.emo;

public class StringTrim {

	public static void main(String[] args) {
		String s = "  ab    c     ";
		s = myTrim(s);
		System.out.println("-"+s+"-");
	}

	public static String myTrim(String s) {
		int start = 0,end = s.length()-1;
		//charAt,根据位置返回
		while(start<=end&&s.charAt(start)==' ') {
			start++;
		}
		//
		while(start<=end&&s.charAt(end)==' ') {
			end--;
		}
		
		return s.substring(start, end+1);
	}

}


猜你喜欢

转载自blog.csdn.net/fighting_future/article/details/80464742