0

I am trying to get data only in English language. enter image description here

I tried different methods like:

public static boolean isAlpha(String s){
return s != null && s.matches("^[a-zA-Z]*$");
}

Or StringUtils as:

public static boolean isAlpha(String str){
    return StringUtils.isAlpha(str);
    }

OR using Java:

public static boolean isAlpha(String s){
        if (s == null) {return false;}
 
        for (int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            if (!(c >= 'A' && c <= 'Z') && !(c >= 'a' && c <= 'z')) {
                return false;
            }
        }
    return true;
    }

Output is: true
I printed the string and found that aforementioned methods are converting Persian String into unicode as: &#1670;&#1591;&#1608;&#1585;&#1740;

Is there anyway to get String of only alphabet letters and spaces?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
User169
  • 81
  • 7
  • Make sure you understand how your input is transformed, to confirm that these are indeed XML numeric entities, then look for a solution to unescape/decode these into the proper Unicode characters. – JayK Apr 08 '21 at 18:09

1 Answers1

0

finally the following piece of code fixed my problem:

public static boolean isAlpha(String str){
        boolean isValid = false;
        for (char c : str.toCharArray()) {
            if (!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(c == ' ')) {
                isValid = true;
                break;
            }
        }
    return isValid;
    }

Its only accepting:
a-z (!(c >= 'a' && c <= 'z') or
A-Z !(c >= 'A' && c <= 'Z') and
white spaces !(c == ' '))

Thanks

User169
  • 81
  • 7