xpath normalize-sapce 函数的Java实现

normalize-space函数实现的功能是:删除字符串前后空格,中间的空格有多个只保留一个。

1. 用Java正则表达式

    public static String normalizeSpace(String str) {
        Pattern WHITESPACE_BLOCK = Pattern.compile("\s+");
        if (str == null) {
            return null;
        }
        return WHITESPACE_BLOCK.matcher(str?.trim()).replaceAll(" ");
    }

2. 一句话

String txt = raw.replaceAll("\s+", " ").trim();    (SO抄来的。。。)

原文地址:https://www.cnblogs.com/longwaytogo/p/6961661.html