JAVA转换Unicode与汉字兼容里面位置有字母数字组合

 

最近倒腾一下百度翻译的api,发现返回的信息是unicode代码的,需要转换一下

1
2
3
4
5
6
7
8
/**
* 字符串转unicode
* @param str
* @return
*/
public static String stringToUnicode(String s) {
String str = "";
for (int i = 0; i < s.length(); i++) {
int ch = (int) s.charAt(i);
if (ch > 255)
// toHexString() 方法返回为无符号整数基数为16的整数参数的字符串表示形式
str += "\\u" + Integer.toHexString(ch);
else
str += "\\" + Integer.toHexString(ch);
}
return str;
}

 

1
2
3
4
5
6
7
8
/**
* unicode转字符串,不转换数字和字母
* @param unicode
* @return
*/
 
public static String unicodeToString(String str) {
Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
Matcher matcher = pattern.matcher(str);
char ch;
while (matcher.find()) {
ch = (char) Integer.parseInt(matcher.group(2), 16);
str = str.replace(matcher.group(1), ch + "");
}
return str;
}

 

https://blog.csdn.net/songylwq/article/details/87890361

文章来源于网络或者作者投稿,若有侵权请联系删除,作者:老钟,如若转载,请注明出处:https://www.laoz.net/1069.html

(1)
上一篇 2020 年 05 月 16 日
下一篇 2020 年 07 月 03 日

相关推荐

  • Linux中 常用命令 和 进程管理

    Vim: 文本编辑器 vim   文件名 vim  + n(行号)  如  vim  + 3  /etc/passwd 光标在第三行; + /word       光标在有word的行首; Vim主要有三种模式: 命令,输入,末行这三种模式 命令模式 —>  输入模式 1...

    vps教程 2011 年 08 月 11 日
  • 2016-9-22 搬瓦工最新优惠信息,最新优惠码

    Bandwagonhost又称为搬瓦工,现在已经被屏蔽了,尽量翻{哈哈}墙访问 老钟用了一段时间,搬瓦工的主机比较稳定,访问速度还算可以 如果搭配国内的CDN使用,访问速度能减少到300毫秒以下,这个速度算是不错了 现在...

    2016 年 09 月 22 日
  • lnmp 运行一段时间后出现nginx 502 Bad Gateway的解决方法

    导致这个问题的原因很多解决:设置一下参数,重启php服务设置参数可以参考:http://www.henry24264.com/?p=1040/home/lnmp/ restart如果重启出现这个问题:Stoping LNMP... Nginx program is...

    vps教程 2012 年 11 月 02 日
  • BURST倒闭了,把网站都迁移到Linode

      前几天收到邮件,burst说7月25停止所有服务,我还以为是我的服务器到期了,看了其他一些VPS大牛的文章,才知道原来burst倒闭了,其实VPS还是可以继续使用的,因为他的VPS卖给了hostwinds ,我的vps是7.25...

    2014 年 06 月 28 日
  • linux下解压zip文件

    安装zip: apt-get install zip unzip命令     语法:unzip [选项] 压缩文件名.zip     各选项的含义分别为:     -x 文件列表 解压缩文件,但不包括指定的file文件。     -v 查看...

    vps教程 2011 年 08 月 02 日
  • CentOS7修改SSH端口

    一、修改ssh配置文件sshd_config [root@bogon ~]# vi /etc/ssh/sshd_config 二、防火墙放行 [root@bogon ~]# firewall-cmd --zone=public --add-port=22345/tcp --permanent [root@bogon ~]# firewall-cmd --relo...

    2020 年 05 月 16 日
  • linux下统计文件夹文件数量

    linux下统计文件夹数量 egrep "7966.org"  file|wc -l 最近bluehost主机上,发现文件限制为50,000个文件。于是想看一下,主机文件夹里的文件数量,由于主机是linux的,所以找了一下。大致有两种: 一种是: ls  ...

    vps教程 2011 年 11 月 23 日
  • 管理VPS ,常用的Linux命令

    系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostname # 查看计算机名 # lspci -tv # 列出所有PCI设备 # lsusb -tv # 列出所...

    vps教程 2011 年 09 月 19 日
  • thinkphp5.0写的项目放到lnmp上出现404错误

    thinphp5.0在Nginx上不适用pathinfo格式的url,在项目的Nginx配置文件里找到include enable-php.conf 改为 include enable-php-pathinfo.conf ,然后就可以了 第一步:打开nginx配置文件 /usr/local/nginx/conf/nig...

    vps教程 2023 年 11 月 28 日
  • lnmp1.7升级php提升失败的解决办法

    ERROR: cannot verify www.php.net's certificate, issued by ‘/C=US/O=Let’s Encrypt/CN=R3’:use `--no-check-certif` 解决方法是安装一个ca-certificates包 sudo yum install -y ca-certificates 然后再次升级...

    vps教程 2023 年 08 月 11 日