问题描述
我想在此方法中为英语单词和日语单词设置不同的过程:
I want to have a different process for English word and Japanese word in this method:
if (english) {
// say english
} else {
// say not english
}
如何在JSP中实现这一点?
How can I achieve this in JSP?
推荐答案
日语字符位于某些Unicode范围内:
Japanese characters lies within certain Unicode ranges:
- U + 3040-U + 309F:平假名
- U + 30A0-U + 30FF:片假名
- U + 4E00-U + 9FBF:汉字
所以你基本上需要做的就是检查角色的代码点是否在已知范围内。
So all you basically need to do is to check if the character's codepoint lies within the known ranges.
Set<UnicodeBlock> japaneseUnicodeBlocks = new HashSet<UnicodeBlock>() {{
add(UnicodeBlock.HIRAGANA);
add(UnicodeBlock.KATAKANA);
add(UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS);
}};
String mixed = "This is a Japanese newspaper headline: ラドクリフ、マラソン五輪代表に1万m出場にも含み";
for (char c : mixed.toCharArray()) {
if (japaneseUnicodeBlocks.contains(UnicodeBlock.of(c))) {
System.out.println(c + " is a Japanese character");
} else {
System.out.println(c + " is not a Japanese character");
}
}
目前还不清楚你什么时候想说日语背部。当字符串包含混合的日语和拉丁语(或其他!)字符时,或者字符串包含仅日语字符时。上面的例子至少应该是一个很好的起点。
It's unclear when exactly you'd like to say Japanese back. When the string contains mixed Japanese and Latin (or other!) characters, or when the string contains only Japanese characters. The above example should at least be a good starting point.
请注意,这一切与JSP完全无关。 只是一种Web表示技术,允许您动态生成HTML / CSS / JS代码。在JSP文件中编写Java代码被视为。
Please note that this all is completely unrelated to JSP. JSP is just a web presentation technology which allows you to generate HTML/CSS/JS code dynamically. Writing Java code inside JSP files is considered a bad practice.
这篇关于如何检查单词是日语还是英语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!