Home >Java >javaTutorial >How to determine whether a string is English in java

How to determine whether a string is English in java

王林
王林Original
2020-05-15 09:33:454100browse

How to determine whether a string is English in java

一般情况下我们都会使用正则表达式来进行判断,具体的正则表达式为【^[a-zA-Z]*】,判断方法的具体代码为【charaString.matches("^[a-zA-Z]*")】。

(视频教程推荐:java视频

具体代码:

public class ChineseAndEnglish {

    // GENERAL_PUNCTUATION 判断中文的"号

    // CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号

    // HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的,号

  /**

   * 是否是中文

   * @param c

   * @return

   */

   public static boolean isChinese(char c) {

        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);

        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS

                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS

                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A

                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION

                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION

                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {

            return true;

        }
        return false;

    }

  /**

   * 是否是英文

   * @param c

   * @return

   */

   public static boolean isEnglish(String charaString){

      return charaString.matches("^[a-zA-Z]*");

    }

  public static boolean isChinese(String str){

      String regEx = "[\\u4e00-\\u9fa5]+";

      Pattern p = Pattern.compile(regEx);

      Matcher m = p.matcher(str);

     if(m.find())

       return true;

     else

       return false;

   }
   
    public static void main(String[] args) {

 System.out.println(isChinese('员'));

System.out.println(isChinese('s'));

 System.out.println(isEnglish("程序员之家"));

System.out.println(isEnglish("robert"));

        System.out.println(isChinese("
程序员论坛"));

    }

推荐教程:java入门程序

The above is the detailed content of How to determine whether a string is English in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn