1. 程式人生 > >Java 去除字串中的空格、回車、換行符、製表符

Java 去除字串中的空格、回車、換行符、製表符

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("");
        }
        returndest;
    } /*-----------------------------------
    笨方法:String s = "你要去除的字串"; 
            1.去除空格:s = s.replace('\\s','');
            2.去除回車:s = s.replace('\n','');
    這樣也可以把空格和回車去掉,其他也可以照這樣做。
    注:\n 回車(\u000a) 
    \t 水平製表符(\u0009) 
    \s 空格(\u0008) 
    \r 換行(\u000d) */ }