Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how check if String has Full width character in java

Can anyone suggest me how to check if a String contains full width characters in Java? Characters having full width are special characters.

Full width characters in String:

abc@gmail.com

Half width characters in String:

[email protected]
like image 463
Kumar Avatar asked Apr 08 '15 07:04

Kumar


People also ask

How do you check if a string contains a character in Java?

The contains() method checks whether a string contains a sequence of characters. Returns true if the characters exist and false if not.

What is a full width character?

In general, Full-width refers to characters where the horizontal and vertical length ratio is the same. Korean, Chinese, and Japanese are full-width by default. Half-width refers to characters where the horizontal and vertical length ratio is 1:2. These characters are horizontally narrow.

How do you check if a string is all letters?

Use the test() method to check if a string contains only letters, e.g. /^[a-zA-Z]+$/. test(str) . The test method will return true if the string contains only letters and false otherwise.

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'm not sure if you are looking for any or all, so here are functions for both:

public static boolean isAllFullWidth(String str) {
    for (char c : str.toCharArray())
      if ((c & 0xff00) != 0xff00)
        return false;
    return true;
}

public static boolean areAnyFullWidth(String str) {
    for (char c : str.toCharArray())
      if ((c & 0xff00) == 0xff00)
        return true;
    return false;
}

As for your half width '.' and possible '_'. Strip them out first with a replace maybe:

String str="abc@gmail.com";

if (isAllFullWidth(str.replaceAll("[._]","")))
  //then apart from . and _, they are all full width

Regex

Alternatively if you want to use a regex to test, then this is the actual character range for full width:

[\uFF01-\uFF5E]

So the method then looks like:

public static boolean isAllFullWidth(String str) {
    return str.matches("[\\uff01-\\uff5E]*");
}

You can add your other characters to it and so not need to strip them:

public static boolean isValidFullWidthEmail(String str) {
    return str.matches("[\\uff01-\\uff5E._]*");
}
like image 162
weston Avatar answered Sep 18 '22 07:09

weston