search
HomeJavajavaTutorialWhat are the methods to determine whether a string contains Chinese characters or characters in Java?

What are the methods to determine whether a string contains Chinese characters or characters in Java?

判断中文字符(包括中文汉字和中文符号)

(1)使用中文的编码区间来判断(推荐,效率高)

String str1 = "T恤;https://blog.csdn.net/vpqtxzmzezeqjj9977";//中文汉字
String str2 = ";https://blog.csdn.net/vpqtxzmzezeqjj9977";//中文分号
String str3 = "https://blog.csdn.net/vpqtxzmzezeqjj9977";//英文
int flage = 0;
for (char c : str2.toCharArray()) {  
         if (c >= 0x4E00 &&  c <= 0x9FA5){
          flage = 1;
          System.out.println("发现中文字符:"+c);
             break; //有一个中文字符就返回  
         }
    }
if(flage!=1){
    System.out.println("恭喜,没有发现中文字符!");
}

相关学习视频推荐:java在线学习

(2)使用正则表达式来判断(不推荐,效率低,需要遍历整个字符串)

for (int i = 0; i < str1.length(); i++) {  
   if (str.substring(i, i + 1).matches("[\\u4e00-\\u9fbb]+")) {  
        flage = 1;
     System.out.println("发现中文字符:"+str.substring(i, i + 1));
        break;  //有一个中文字符就返回  
   }  
}  
if(flage!=1){
    System.out.println("恭喜,没有发现中文字符!");
}

(3)使用Unicode编码来判断中文标点(推荐)

// GENERAL_PUNCTUATION 判断中文的“号  
// CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号  
// HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的,号  
private static final 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;  
}

更多相关文章教程可以访问:java开发入门

The above is the detailed content of What are the methods to determine whether a string contains Chinese characters or characters in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools