Replace Unicode encoding with Chinese characters

You can use regular expressions and string replacement methods in the Java language to implement this conversion function, and the code is as follows:

String input = "## added by mgr on 2019-12-25\n" + "##\\u662F\\u5426\\u5F00\\u542F\\u9A8C\\u8BC1\\u7801\n" + "auth.client.openCaptcha=false\n"
    + "\n" + "##\\u4E0D\\u9700\\u8981\\u62E6\\u622A\\u7684 uri\n"
    + "#auth.client.anonUri=/static/**,/captcha/**,/authorize,/accessToken,/index,/third/**,/pwd/**,/sys/forgotPwd**,/sys/forgotPwd/**,/dict/**\n"
    + "auth.client.anonUri=/static/**,/captcha/**,/authorize,/accessToken,/,/index,/index/**,/reg/**,/pwd,/pwd/**,/mb/**,/loginpwd,/loginpwd/**,/user/registered,/sys/forgotPwd/**,/dict/**,/vldCode/**,/getLoginConfig,/heartbeat\n"
    + "##\\u9700\\u8981\\u767B\\u5F55\\u540E\\u8BBF\\u95EE\\u7684\\u5730\\u5740\n" + "auth.client.userUrl=/xxx/**\n" + "\n"
    + "#############\\u5BA2\\u6237\\u7AEFCookie\\u914D\\u7F6E#############\n" + "#cookie\\u5171\\u4EAB\\u57DF\n"
    + "auth.client.cookie.domain=\n" + "#cookie\\u662F\\u5426https\n" + "auth.client.cookie.secure=false\n"
    + "#cookie\\u5171\\u4EAB\\u8DEF\\u5F84\n" + "auth.client.cookie.path=/\n" + "#cookie\\u9ED8\\u8BA4\\u8FC7\\u671F\\u65F6\\u95F4(\\u79D2)\n"
    + "auth.client.cookie.timeout=3600";
String regex = "\\\\u([0-9a-fA-F]{4})";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
    
    
    String unicode = matcher.group(1);
    char ch = (char)Integer.parseInt(unicode, 16);
    matcher.appendReplacement(sb, String.valueOf(ch));
}
matcher.appendTail(sb);
String output = sb.toString();
System.out.println(output);

Here, the regular expression "\u([0-9a-fA-F]{4})" is used to match the Unicode encoding part in the input string, and then the Unicode encoding is converted into Chinese characters, and finally the Unicode encoding is realized through the string replacement method. The code is replaced with the function of Chinese characters.

Results of the

## added by mgr on 2019-12-25
##是否开启验证码
auth.client.openCaptcha=false

##不需要拦截的 uri
#auth.client.anonUri=/static/**,/captcha/**,/authorize,/accessToken,/index,/third/**,/pwd/**,/sys/forgotPwd**,/sys/forgotPwd/**,/dict/**
auth.client.anonUri=/static/**,/captcha/**,/authorize,/accessToken,/,/index,/index/**,/reg/**,/pwd,/pwd/**,/mb/**,/loginpwd,/loginpwd/**,/user/registered,/sys/forgotPwd/**,/dict/**,/vldCode/**,/getLoginConfig,/heartbeat
##需要登录后访问的地址
auth.client.userUrl=/xxx/**

Guess you like

Origin blog.csdn.net/ChinaLiaoTian/article/details/130492987