Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Validate half-width Japanese character

This question might have been asked several times but I didn't find a proper answer so I am posting this question. What I want to do is validate the Japanese text entered in the edit text field to allow only half-width Japanese characters. I only want to check the validation once user enters the text and taps on some action button.

like image 459
viper Avatar asked Oct 30 '20 13:10

viper


People also ask

How to type half-width katakana?

To convert a string into half-width Katakana characters, press [F9].

What is a half-width character?

Half-width refers to characters where the horizontal and vertical length ratio is 1:2. These characters are horizontally narrow. English letters, numbers, spaces, and punctuation marks such as comma and period are half-width by default.

How to get full width katakana?

For Windows: Use F10 within an online form to toggle quickly between full-width and half-width characters. For Mac users: Full-width, zenkaku katakana, is control + k.

What is full width alphanumeric?

Adjective. fullwidth (not comparable) (computing, typography) Of a text character, occupying the space of two alphanumeric characters in a monospace font, or two "normal" text columns.


1 Answers

I've created method validates if typed character is half-width kana:

public boolean validate(String c) {
    Pattern pattern = Pattern.compile("[\uff61-\uff9f]");
    return pattern.matcher(c).matches();
}

I understood that you want to check is written text composed of only half-width kana, yes? To do this, in onClick() of button which you clicking for validate, write something like this:

for (int i = 0; i < textToValidate.length(); i++) {
    if (validate(textToValidate.charAt(i))) {
        continue;
    } else {
        System.out.println("Text wasn't written in half-width kana.");
        return;
    }
}
System.out.println("Text was written in half-width kana.");

Let me know if my answer is helpful for you. ;)

like image 196
fr3ddie Avatar answered Oct 10 '22 21:10

fr3ddie