


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!

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

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

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

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]

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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
Visual web development tools