半角空格:\u0020 英文符號 代碼中常用
全角空格:\u3000 中文符號 中文文章中常用
不間斷空格:\u00A0 不間斷空格 主要在 office 中使用,讓一個單詞在結尾處不會換行顯示,快捷鍵 Ctrl + Shift + Space
半角空格和不間斷空格肉眼是分辨不出來的,但是程序能夠分辨出來。
public class Test { public static void main(String[] args) { System.err.println("Hello\u00A0World"); // Hello World System.err.println("Hello\u0020World"); // Hello World 常用 System.err.println("Hello\u3000World"); // Hello World String spaceA = " "; // 不間斷空格 String spaceB = " "; // 半角空格 String spaceC = " "; // 全角空格 System.err.println(spaceA.equals(spaceB)); // false System.err.println(spaceA.equals(spaceC)); // false System.err.println(spaceB.equals(spaceC)); // false System.err.println("\u00A0".equals(spaceA)); // true System.err.println("\u0020".equals(spaceB)); // true System.err.println("\u3000".equals(spaceC)); // true } }
將字符串中的不連續轉變成半角空格
String str = "Hello\u00A0world" str = str.replace('\u00A0', '\u0020'); // 將不間斷空格替換為半角空格
在 Java Web 開發中遇到時可通過添加過濾器的方式來解決
過濾器代碼如下:
package pathogen.filter; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; public class SpecialCharacterFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// 采用 Request 包裝類包裝 request 的方法,攔截獲取參數的方法,替換特殊字符。 chain.doFilter(new SpecialCharacterRequestWrapper((HttpServletRequest)request), response); } } class SpecialCharacterRequestWrapper extends HttpServletRequestWrapper { public SpecialCharacterRequestWrapper(HttpServletRequest request) { super(request); } @Override public String getParameter(String name) { return replaceSpecialCharacter(super.getParameter(name)); } @Override public Map<String, String[]> getParameterMap() { Map<String, String[]> originalParameters = super.getParameterMap(); Map<String, String[]> newParameters = new HashMap<>(originalParameters.size()); // Lambda 表達式 + forEach() 遍歷 Map originalParameters.forEach((k, v) -> newParameters.put(k, replaceSpecialCharacter(v))); // v 的數據類型是 String [] return newParameters; } @Override public String[] getParameterValues(String name) { return replaceSpecialCharacter(super.getParameterValues(name)); } private String replaceSpecialCharacter(String str) { return str == null ? null : str.replace("\u00A0", "\u0020"); // 將不連續空格替換為半角空格 } private String [] replaceSpecialCharacter(String[] str) { if (str == null) { return str; } for (int i = 0; i < str.length; i++) { str[i] = replaceSpecialCharacter(str[i]); } return str; } }
在部署描述符(web.xml)中配置過濾器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>DataEntryServlet</servlet-name> <servlet-class>pathogen.controller.DataEntryServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DataEntryServlet</servlet-name> <url-pattern>/entry</url-pattern> </servlet-mapping> <servlet> <display-name>QueryServlet</display-name> <servlet-name>QueryServlet</servlet-name> <servlet-class>pathogen.controller.QueryServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>QueryServlet</servlet-name> <url-pattern>/query</url-pattern> </servlet-mapping> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>pathogen.controller.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>SpecialCharacterFilter</filter-name> <filter-class>pathogen.filter.SpecialCharacterFilter</filter-class> </filter> <filter-mapping> <filter-name>SpecialCharacterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>