java去除字符串中的空格、回车、换行符、制表符

来源:互联网 发布:初中英语点读软件 编辑:程序博客网 时间:2024/05/19 12:13
import java.util.regex.Matcher;
02import java.util.regex.Pattern;
03 
04 
05 
06/**
07 * @author lei
08 * 2011-9-2
09 */
10public class StringUtils {
11 
12    public static String replaceBlank(String str) {
13        String dest = "";
14        if (str!=null) {
15            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
16            Matcher m = p.matcher(str);
17            dest = m.replaceAll("");
18        }
19        return dest;
20    }
21    public static void main(String[] args) {
22        System.out.println(StringUtils.replaceBlank("just do it!"));
23    }
24    /*-----------------------------------
25 
26    笨方法:String s = "你要去除的字符串";
27 
28            1.去除空格:s = s.replace('\\s','');
29 
30            2.去除回车:s = s.replace('\n','');
31 
32    这样也可以把空格和回车去掉,其他也可以照这样做。
33 
34    注:\n 回车(\u000a)
35    \t 水平制表符(\u0009)
36    \s 空格(\u0008)
37    \r 换行(\u000d)*/
38}
原创粉丝点击