Java is a small example of removing Spaces carriage returns line breaks and tabs from a string

  • 2020-04-01 02:06:07
  • OfStack


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("just do it!"));
    }
    /*-----------------------------------
     Stupid method: String s = " The string you want to remove ";
. Remove Spaces: s = s.replace('\s','');
. Remove return: s = s.replace('n','');
     You can also remove the space and the carriage return, and you can do the same for others. 
     Note: n  enter (u000a) 
    t  Horizontal tabs (u0009) 
    s  The blank space (u0008) 
    r  A newline (u000d)*/
}


Related articles: