ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; /** * @authorZeze * 2016-08-15*/ Public classStringUtils { Public Staticstring Replaceblank (String str) {string dest= ""; if(str!=NULL) {Pattern P= Pattern.compile ("\\s*|\t|\r|\n"); Matcher m=P.matcher (str); Dest= M.replaceall (""); } returndest; } Public Static voidMain (string[] args) {System.out.println (Stringutils.replaceblank ("Just do it!")); } /*-----------------------------------Stupid method: string s = "You want to remove the strings"; 1. Remove Space: s = s.replace (' \\s ', '); 2. Remove carriage return: s = s.replace (' \ n ', '); You can also remove the space and carriage return, and others can do so. Note: \ n Enter (\u000a) \ t horizontal tab (\U0009) \s space (\u0008) \ R line Break (\u000d)*/}
Java removes spaces, carriage returns, line feeds, tab characters from strings