Java - How to check string is chinese
July 19, 2023
Here is a code to check string contains Chinese characters or not.
package com.example.demo;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestAppApplication implements CommandLineRunner{
public static void main(String[] args) {
SpringApplication.run(TestAppApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Pattern p = Pattern.compile("\\p{IsHan}");
Matcher m = p.matcher("你好");
System.out.println(m.find());
}
}