Java字符串空格处理(无论有几个空格,都输出为1个空格,去除字符串前后的全角空格)

1、无论有几个空格,都输出为1个空格:

public class DeleteSpace { public static void main(String[] args)throws Exception { String string="new ada dads a "; System.out.println(string.replaceAll(" {2,}", " "));//一个函数搞定 StringBuffer sb=new StringBuffer();//用其他方法实现 int flag; for(int i=0;i

 

2、去除字符串前后的全角空格:

public static void main(String[] args) { String a = " aaaa aaaa "; a = a.trim(); while(a.startsWith(" ")){ a = a.substring(1,a.length()).trim(); } while(a.endsWith(" ")){ a = a.substring(0,a.length()-1).trim(); } System.out.println(a); }

你可能感兴趣的:(JAVA,SE)