如何替换 tomcat 和 websphere 上的空格

How to replace whitespace on both tomcat and websphere

开发人员:Tomcat 和 Oracle JDK 1.6 测试:WebSphere 和 IBM JDK 1.6

提交到网络服务时,字符串以不间断 space (U+00A0/#160) 结尾。
在 Tomcat 中,下面的解决方案有效,但是在 WebSphere 中,不间断 space 作为 (U+00C2/#194).

出现
public static String removeWhiteSpace(String inputString) {
    return inputString == null ? null : inputString.replaceAll("[\s\u00A0]+", "");
}

要使这项工作在 tomcat 和 WebSphere 中都有效,您需要做什么。

我们最终采用的解决方案是替换两种可能的输出:

\u00C2\u00A0 替换了我们 WebSphere 环境中的 No-Break space。
\u00A0 在我们的 tomcat 环境中替换了 No-Break space。
\s 替换任何正常的白色 space 字符。

中Java:

inputString.replaceAll("(\u00C2\u00A0|\u00A0|\s)+", "")

缺点:

在 Tomcat 中,如果 u00C2 字符紧接在不间断 space 之前,则可能会意外替换该字符。但是,风险被认为是可以接受的。