1 Packagecom.ynet.utils;2 3 ImportJava.util.regex.Matcher;4 ImportJava.util.regex.Pattern;5 6 /**7 * Created by Arya on 2017/11/3 0003.8 */9 Public classStringutil {Ten //Remove all spaces One Public Staticstring Replaceallblank (String str) { AString s = ""; - if(str!=NULL) { -Pattern p = pattern.compile ("\\s*|\t|\r|\n"); the /*\ n Enter (\u000a) - \ t Horizontal tab (\u0009) - \s Space (\u0008) - \ R line Break (\u000d)*/ +Matcher m =P.matcher (str); -s = M.replaceall (""); + } A returns; at } - //Remove all spaces and leave a - Public Staticstring Replaceblankleaveone (String str) { -String s = ""; - if(str!=NULL) { -Pattern p = pattern.compile ("\\s{2,}|\t|\r|\n"); inMatcher m =P.matcher (str); -s = M.replaceall (""); to } + returns; - } the * Public Static voidMain (string[] args) { $System.out.println (Stringutil.replaceallblank ("Just do it!"));Panax NotoginsengSystem.out.println (Stringutil.replaceblankleaveone ("Just do it!")); - } the +}
Operating effect:
Java removes whitespace, line breaks, and so on in string