Java实现字符串和unicode互转

             Java实现字符串和unicode互转 Java字符串转unicode

 

一、前言

看一篇文章时候,发现一个有意思的问题,居然输出了“hello world!”,仔细一看发现 “\u000a”,是unicode ,那么来研究下 unicode的转换问题。

 

 

public static void main(String[] args) {
  String a = "Hello";
  // \u000d a="world";
  System.out.println(a);
  // \u000a a="hello world!";
  System.out.println(a);
}

 

二、原生方法实现 字符串转Unicode

1、代码如下,引用自这里

/**
	 * description: 字符串转 Unicode --- 原生方法
	 * @param str
	 * @return String
	 * @version v1.0
	 * @author w
	 * @date 2021年4月21日 上午10:18:25
	 */
	public static String stringToUnicode(String str) {
		StringBuffer sb = new StringBuffer();
		char[] c = str.toCharArray();
		for (int i = 0; i < c.length; i++) {
			sb.append("\\u" + Integer.toHexString(c[i]));
		}
		return sb.toString();
	}
 
	/**
	 * description: Unicode 转字符串  --- 原生方法
	 * @param unicode
	 * @return String
	 * @version v1.0
	 * @author w
	 * @date 2021年4月21日 上午10:18:57
	 */
	public static String unicodeToString(String unicode) {
		StringBuffer sb = new StringBuffer();
		String[] hex = unicode.split("\\\\u");
		for (int i = 1; i < hex.length; i++) {
			int index = Integer.parseInt(hex[i], 16);
			sb.append((char) index);
		}
		return sb.toString();
	}

 

三、使用工具类实现

1、依赖pom如下:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8.1</version>
</dependency>

 

2、使用StringEscapeUtils工具类实现转换,如下:

/**
	 * description: 字符串  转 Unicode --- 使用工具类实现一键转换
	 * @param str
	 * @return String
	 * @version v1.0
	 * @author w
	 * @date 2021年4月21日 上午10:21:55
	 */
	public static String strToUnicode(String str) {
		return StringEscapeUtils.escapeJava(str);
	}
	
	/**
	 * description:  Unicode 转字符串  --- 使用工具类实现一键转换
	 * @param unicode
	 * @return String
	 * @version v1.0
	 * @author w
	 * @date 2021年4月21日 上午10:21:02
	 */
	public static String unicodeToStr(String unicode) {
		return StringEscapeUtils.unescapeJava(unicode);
	}

 

四、测试

    @Test
	public void test() {
		String str = "爱我中华";
		String stringToUnicode = stringToUnicode(str);
		System.out.println(stringToUnicode);
		String unicodeToString = unicodeToString(stringToUnicode);
		System.out.println(unicodeToString);
	}
	
	@Test
	public void test2() {
		String str = "爱我中华";
		String strToUnicode = strToUnicode(str);
		System.out.println(strToUnicode);
		String unicodeToStr = unicodeToStr(strToUnicode);
		System.out.println(unicodeToStr);
	}

 

五、总结

1、unicode和字符串转换,可以通过原生方法实现,也可以通过StringEscapeUtils工具类实现。

2、遇到StringEscapeUtils类过期的问题,可以将依赖pom调整为:commons-text

 

 

 

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-text -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.9</version> 
</dependency>

 

 

 

 

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值