8

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:

abc@gmail.com
Adrian Thompson Phillips
  • 6,893
  • 6
  • 38
  • 69
Kumar
  • 1,139
  • 2
  • 8
  • 9
  • the contains method will tell you that. iterate over a String containing only those "full width chars", and for each, run the contains method on your String. – Stultuske Apr 08 '15 at 07:56
  • Yeah i checked in that way too. But code looks messy, However in above case am checking for email. so i have to check for some additional characters like @ , . , _ etc – Kumar Apr 08 '15 at 08:11
  • Are you looking for **all** full width, or **any** full width? i.e. what result do you want when mixture of full and half? – weston Apr 08 '15 at 08:15
  • I want to check if String contains of any full width characters. that may be aphabet, numeric or special charater. – Kumar Apr 08 '15 at 08:38

5 Answers5

5

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._]*");
}
weston
  • 54,145
  • 21
  • 145
  • 203
  • Thank you for your code. But am not supposed to ignore "." , " _ " and any of the special characters. Because they can be in full width too. Can you please let me know the range of Unicode for full width including special characters too.. – Kumar Apr 08 '15 at 11:01
  • 1
    Well your example does not include the full width version of period. And I've given you the full range of full width characters. – weston Apr 08 '15 at 11:03
  • `str.matches("[\\uff01-\\uff5E]*")` Should do it. If it doesn't, it's probably because it really isn't all full width, like the example you've posted. – weston Apr 08 '15 at 11:04
2

You can compare the UNICODE Since unicode for alphabets (a-z) is 97-122 , So you can easily diffrentiate between the two

String str="abc@gmail.com";
System.out.println((int)str.charAt(0));

for Input

abc@gmail.com

Output

65345
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
  • 1
    Thank you for your reply.. But in case of checking email string '@' need to be considered too along with ".", "_" etc. Are there any implicit methods that i can check with. – Kumar Apr 08 '15 at 08:09
  • #Neeraj : can you please also let me know the Unicode range for all full width characters. – Kumar Apr 08 '15 at 08:33
2

You can try something like this:

public static final String FULL_WIDTH_CHARS = "AaBbCcDdEeFfGgHhIiJj"
                      + "KkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";

public static boolean containsFullWidthChars(String str) {
    for(int i = 0; i < FULL_WIDTH_CHARS.length(); i++) {
        if(str.contains(String.valueOf(FULL_WIDTH_CHARS.charAt(i)))) {
            return true;
        }
    }
    return false;
}
ByteHamster
  • 4,884
  • 9
  • 38
  • 53
0

use regular expression here. \W is used to check for non-word characters.

str will contain full width character if following statement return true:

boolean flag = str.matches("\\W");

  • Since am checking emails. i need to check if any special characters are of full width too. – Kumar Apr 08 '15 at 08:36
0

half-width: 1 byte

full-width: > 1 byte (2,3,4.. byte)

-> compare: length of String == byte length

String strCheck = "abc@gmail.com";
if (str.length() != str.getBytes().length) {
    // is Full Width
} else {
    // is Half Width
}
hieunt
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 17 '22 at 04:05