从字符串构建char [英] Build char from string
本文介绍了从字符串构建char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想允许用户通过XML配置文件指定Unicode范围。例如。他们可以指定0100..017F作为范围。对于我的(Java)应用程序使用这个字符范围,我需要将XML输入(String)转换为类型char。任何想法?#
I want to allow a user specify a Unicode range via an XML config file. E.g. they could state 0100..017F as the range. For my (Java) app to consume this char range, I need to convert the XML input (String) to type char. Any ideas?#
Eg
String input = "0100..017F"; // I can change format of input, if enables a solution
char from = '\u0100';
char to = '\u017f';
谢谢。
推荐答案
如果它始终与该格式完全匹配,则这将足够:
If it always matches exactly that format, then this will suffice:
char from = (char)Integer.parseInt(input.substring(0, 4), 16);
char to = (char)Integer.parseInt(input.substring(6), 16);
更灵活的功能:
char from;
char to;
java.util.regex.Matcher m = java.util.regex.Pattern.compile(
"^([\\da-fA-F]{1,4})(?:\\s*\\.\\.\\s*([\\da-fA-F]{1,4}))?$").matcher(input);
if (!m.find()) throw new IllegalArgumentException();
from = (char)Integer.parseInt(m.group(1), 16);
if (m.group(2) != null) {
to = (char)Integer.parseInt(m.group(2), 16);
} else {
to = from;
}
这允许每个字符使用1到4个十六进制数字, ..
可能在其周围有空格,并且范围的到
部分可以省略,并假设等于来自
。
That allows for 1 to 4 hex digits for each character, the ..
may have space around it, and the to
part of the range may be omitted and assumed to be equal to from
.
这篇关于从字符串构建char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文