去掉字符串中特殊符号造成的空格

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("hello zyf"));
        System.out.println("hello \n zyf");
        System.out.println("hello \t zyf");
        System.out.println(StringUtils.replaceBlank("hello \n zyf"));
    }
    
}

猜你喜欢

转载自www.cnblogs.com/zyf-yxm/p/10557718.html
今日推荐