不可見字元“\u200b“的坑
阿新 • • 發佈:2021-02-12
技術標籤:java
US,MY,CN,VN,TH,SG,HK,TW,IN,ID,AU,JP,KR
仔細觀察上面字串是不存在空格字元的,但是程式碼匹配的時候出錯了,copy到瀏覽器控制檯中,發現多了"\u200b"
copy到idea中可以清晰看到末尾多了"\u200b":
String cooConfigStr1 = "US,MY,CN,VN,TH,SG,HK,TW,IN,ID,AU,JP,KR\u200B";
使用Str.trim()是去除不掉的,這是需要使用replace,replace("\u200B", “”),正確的程式碼示例姿勢:
String originCountry = "KR";
originCountry = originCountry.trim().toUpperCase();
String cooConfigStr1 = "US,MY,CN,VN,TH,SG,HK,TW,IN,ID,AU,JP,KR\u200B";
System.out.println("替換前長度:" + cooConfigStr1.length());
cooConfigStr1 = cooConfigStr1. trim().replace("\u200B", "");
System.out.println("替換後長度:" + cooConfigStr1.length());
if(!cooConfigStr.equals(null) && !"*".equals(cooConfigStr)){
List<String> cooList1 = Arrays.asList(cooConfigStr1.split("\\,") );
if(!cooList1.contains(originCountry)){
System.out.println("不在白名單中");
}else{
System.out.println("在白名單中");
}
}
執行結果如下: