How to determine a string is English or Persian?

后端 未结 5 923
南笙
南笙 2021-01-11 17:45

I have edittext in a form, I want that when the user inputs text into the edittext for my program to detect which language was inserted into the edittext.

Is there a

5条回答
  •  生来不讨喜
    2021-01-11 18:19

    You can know a string is english or persian by using Regex.

    public static final Pattern VALID_NAME_PATTERN_REGEX = Pattern.compile("[a-zA-Z_0-9]+$");
    
    public static boolean isEnglishWord(String string) {
        return VALID_NAME_PATTERN_REGEX.matcher(string).find();
    }
    

    this only works with words and numbers. if there is a character like '=' or '+' , the function would return false . you can fix that by editing the regex to match what you need .

提交回复
热议问题