import java.util.regex.Matcher; import java.util.regex.Pattern; public class StringUtils { public static String replaceBlank(String str) { String dest = ""; if (str!=null) { Pattern p = Pattern.compile("\\s*|\t|\r|\n");//有些特殊符号如制表符会造成返回报文出现空格 Matcher m = p.matcher(str); dest = m.replaceAll(""); } return dest; } public static void main(String[] args) { System.out.println(StringUtils.replaceBlank("hello zyf")); System.out.println("hello \n zyf"); System.out.println("hello \t zyf"); System.out.println(StringUtils.replaceBlank("hello \n zyf")); } }