是否可以在Java中动态生成Unicode字符?(Is it possible to dynamically generate Unicode characters in Java? [duplicate])
这个问题在这里已有答案:
基本上,我想在
System.out.println
行中使用Unicode符号,但这些Unicode字符需要动态生成。例如,取符号
U+2074
,U+2075
和U+2076
,它们分别返回x^4
,x^5
和x^6
。 要用Java打印它我会使用\u2074
,\u2075
和\u2076
,但如果我想根据另一个值生成它们(即\u207x
,其中x
是变量)怎么办?如果我使用PHP,我会使用变量变量 ,这将非常方便。
我甚至考虑使用hack如将
u207
保存为String,对x
的值进行计算并将此值附加到字符串,但是在字符串中引入\
符号时会出现问题:String unicodeBase = "u207"; // I do something to get a value for x String unicodeChar = "u207" + x; System.out.println("\unicodeChar");
如果我这样做,我得到一个
illegal unicode escape
错误,这是预期的。 但是,如果我转义\
符号,那么输出是一个未处理的字符串:System.out.println("\\unicodeChar");
这将返回字符串
unicodeChar
。那么,是否可以在Java中动态生成Unicode字符?
This question already has an answer here:
Basically, I would like to use Unicode symbols in my
System.out.println
lines, but these Unicode characters would need to be dynamically generated.For example, take symbols
U+2074
,U+2075
andU+2076
, which return, respectively,x^4
,x^5
andx^6
. To print them in Java I would use\u2074
,\u2075
and\u2076
, but what if I want to generate them based on another value (i.e.\u207x
wherex
is a variable)?If I were using PHP, i would use variable variables for this which would be extremely handy.
I even went as far as thinking about using a hack like saving
u207
as a String, making my calculations on the value ofx
and appending this value to the string, but then there are issues introducing the\
symbol inside a string:String unicodeBase = "u207"; // I do something to get a value for x String unicodeChar = "u207" + x; System.out.println("\unicodeChar");
If i do this, I get an
illegal unicode escape
error, which is expected. But then, if I escape the\
symbol, then the output is a string that is not processed:System.out.println("\\unicodeChar");
This returns the string
unicodeChar
.So, is it possible to dynamically generate Unicode characters in Java?
原文:https://stackoverflow.com/questions/36182355
最满意答案
table.sort(list, function(a, b) return a[1] < b[1] end)
table.sort
的第二个参数是一个排序函数。 因此,在这种情况下,我们将查看a
和b
的第一项,并使用它进行比较。table.sort(list, function(a, b) return a[1] < b[1] end)
The second argument to
table.sort
is a sorting function. So in this case we look into the first item ina
andb
and use that to compare.
相关问答
更多-
Python和Lua是什么?[2023-10-21]
Python Python(发音:[ 'paiθ(ə)n; (US) 'paiθɔn ]),是一种面向对象的解释性的计算机程序设计语言,也是一种功能强大而完善的通用型语言,已经具有十多年的发展历史,成熟且稳定。Python 具有脚本语言中最丰富和强大的类库,足以支持绝大多数日常应用。 这种语言具有非常简捷而清晰的语法特点,适合完成各种高层任务,几乎可以在所有的操作系统中运行。 目前,基于这种语言的相关技术正在飞速的发展,用户数量急剧扩大,相关的资源非常多。 Python的Hello World程序 ... -
Python和Lua是什么?[2022-07-16]
Python Python(英语发音:/ˈpaɪθən/), 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年,Python 源代码同样遵循 GPL(GNU General Public License)协议。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。常见的一种应用情形是,使用Python快速生成程序的原型(有时甚至是程序的最 ... -
这个怎么样? tbl = { { 'etc3', 1336 }, { 'etc2', 14477 }, { 'etc4', 1335 }, { 'etc1', 1337 }, } table.sort(tbl, function(a, b) return a[2] > b[2] end) for k,v in ipairs(tbl) do print(v[1], ' == ', v[2]) end 以这种方式组织数据使得排序更容易,并注意我只调用table.so ...
-
对于Lua上的循环(For Loop on Lua)[2022-02-07]
你的问题很简单: names = {'John', 'Joe', 'Steve'} for names = 1, 3 do print (names) end 该代码首先声明一个名为names的全局变量。 然后,你开始一个for循环。 for循环声明一个局部变量,恰好恰好也称为names ; 变量之前已经用names定义的事实完全是无关紧要的。 在for循环中任何使用names都会引用本地名称 ,而不是全局名称。 for循环表示循环的内部部分将被调用, names = 1 ,然后是names = 2 ... -
排序lua列表(Sorting lua lists)[2023-09-24]
table.sort(list, function(a, b) return a[1] < b[1] end) table.sort的第二个参数是一个排序函数。 因此,在这种情况下,我们将查看a和b的第一项,并使用它进行比较。 table.sort(list, function(a, b) return a[1] < b[1] end) The second argument to table.sort is a sorting function. So in this case we look int ... -
Lua如何处理堆栈?(How Lua deal with the stack?)[2022-04-30]
它以不同的方式获取不同的数据:代码更改对象内部的数据。 struct Object { int val; }; void more_stuff(Object *the_data) { //the_data->val has 5 in it now. } void do_stuff(Object *the_data) { int old_val = the_data->val; the_data->val = 5; more_stuff(the_data); the_data->v ... -
Lua中的链接列表(Linked Lists in Lua)[2022-02-03]
你在“记忆中被覆盖”是什么意思? 你所做的一切都不会导致这种情况发生。 让我们一步一步看看你在做什么。 local head = nil 现在存在一个名为head的局部变量。 它的值nil 。 head = {next = head, value = "d"} 让我们把它分解为这里的操作顺序。 这相当于以下内容: do local temp = {} temp.next = head --This is still `nil` temp.value = "d" head = temp ... -
您的新表需要连续的整数键,并且需要将值本身作为表。 所以你想要这个订单上的东西: SortFunc = function (myTable) local t = {} for title,value in pairsByKeys(myTable) do table.insert(t, { title = title, value = value }) end myTable = t return myTable end 这假设pairsByKeys做 ...
-
你似乎知道table.sort是如何工作的,所以你在哪里被困? 只需创建一个查找表,将优先级名称转换为整数并进行比较: priorities = {high = 2, medium = 1, low = 0} table.sort(t, function(e1, e2) return priorities[e1[2]] > priorities[e2[2]] end) You seem to know how table.sort works, so where are you stuck? Si ...
-
正如Lua要求的man页面所描述的 ,它在路径中搜索文件。 这个路径可以在C中定义。看看这篇文章: “从C ++ / C设置全局LUA_PATH变量” require函数非常实用,可以加载.lua文件中定义的模块和库。 As decribed by the Lua require man page, it searches for the file in a path. This path can be defined in C. Have a look to this post : "Setting th ...