出于某种原因,一段代码将空格替换为\u00A0- 即不间断空格。然后使用此代码来清理URL(是的,我知道这很糟糕 - 在很多方面)。奇怪的是,当这些显示在我的测试 jsp 中时,会Â出现一个流氓- 为什么?
示例 JSP 来演示该问题。
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<%
String[] parameters = request.getParameterValues("p");
if (parameters == null || parameters.length == 0) {
parameters = new String[]{""};
}
%>
</head>
<body>
<h1>Hello World!</h1>
<a href='index.jsp?p=<%="Hello\u00A0there"%>'>A Link</a>
<p><%=parameters[0]%></p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
为什么参数显示为Hello there?哪里c2来的?
添加
顺便说一句:的十六进制parameter被48 65 6c 6c 6f c2 a0 74 68 65 72 65示出c2原位。
要回答实际问题“ \xc3\x82 (C2) 来自哪里? ”,您可能会发现这篇文章很有帮助
\n不间断空格,0x00A0在UTF-16中,编码为0xC2A0UTF -8。
这张表也可能有帮助
\n\n\n\n编码 Unicode 字符的示例(以十六进制表示法)
\n\nRun Code Online (Sandbox Code Playgroud)\n16-bit Unicode UTF-8 Sequence\n0001 01\n007F 7F\n0080 C2 80 <-- this was the case of nbsp\n07FF DF BF\n0800 E0 A0 80\nFFFF EF BF BF\n010000 F0 90 80 80\n10FFFF F4 8F BF BF\n