从其编号创建Unicode字符 [英] Creating Unicode character from its number

查看:110
本文介绍了从其编号创建Unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java中显示Unicode字符。如果我这样做,它的工作很好:



字符串符号=\\\∂;



符号等于∂。这是我想要的。



问题是,我知道Unicode号码,需要从中创建Unicode符号。我尝试(对我)显而易见的事情:

  int c = 2202; 
String symbol =\\u+ c;

但是,在这种情况下,符号等于\\\∂。这不是我想要的。



如果我知道它的Unicode编号(但只在运行时---我不能硬编码 c> c>

char 。您可以使用 Character.toString()将它转换为 String

  String s = Character.toString((char)c); 

编辑:



Java源代码中的转义序列( \u 位)是十六进制的,因此如果你想重现一个转义序列,你需要像 int c = 0x2202


I want to display a Unicode character in Java. If I do this, it works just fine:

String symbol = "\u2202";

symbol is equal to "∂". That's what I want.

The problem is that I know the Unicode number and need to create the Unicode symbol from that. I tried (to me) the obvious thing:

int c = 2202;
String symbol =  "\\u" + c;

However, in this case, symbol is equal to "\u2202". That's not what I want.

How can I construct the symbol if I know its Unicode number (but only at run-time---I can't hard-code it in like the first example)?

解决方案

Just cast your int to a char. You can convert that to a String using Character.toString():

String s = Character.toString((char)c);

EDIT:

Just remember that the escape sequences in Java source code (the \u bits) are in HEX, so if you're trying to reproduce an escape sequence, you'll need something like int c = 0x2202.

这篇关于从其编号创建Unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆