5

I want to have a different process for English word and Japanese word in this method:

if (english) {
    // say english
} else {
    // say not english
}

How can I achieve this in JSP?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Kareem Nour Emam
  • 1,054
  • 4
  • 14
  • 27

2 Answers2

14

Japanese characters lies within certain Unicode ranges:

  • U+3040–U+309F: Hiragana
  • U+30A0–U+30FF: Katakana
  • U+4E00–U+9FBF: Kanji

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.

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • how can make this with english if chrachter not english? – Kareem Nour Emam Feb 04 '12 at 21:43
  • can the same be done for objective-c..? I wan't to do this for russian language... please see my question here.. http://stackoverflow.com/questions/9972055/how-to-check-if-a-char-lies-between-a-certain-unicode-range – Ankit Srivastava Apr 02 '12 at 07:12
0

AFAIK, Japanese words use chars above 256, whereas English doesn't use them. You could test if one of the chars is >= 256 in the word.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255