使用java代码判断一个字符串里是否包含中文字符

测试代码:

package unicode;

import org.apache.commons.lang3.StringUtils;



public class ChineseTest {

	public static void main(String[] args) {
		ChineseTest tool = new ChineseTest();
		System.out.println(tool.containsChineseCharacter("Helloab!?12D#asDSF!&#$"));
		System.out.println(tool.containsChineseCharacter("你好	"));

	}
	
	protected boolean containsChineseCharacter(final String s)
	{
		if (StringUtils.isNotEmpty(s))
		{
			for (int i = 0; i < s.length();)
			{
				final int codepoint = s.codePointAt(i);
				System.out.println("Code Point: " + codepoint);
				i += Character.charCount(codepoint);
				System.out.println("i: " + i + " CharCount: " + Character.charCount(codepoint));
				if (Character.UnicodeScript.of(codepoint) == Character.UnicodeScript.HAN)
				{
					return true;
				}
			}
		}
		return false;
	}

}

输出:

Code Point: 72
i: 1 CharCount: 1
Code Point: 101
i: 2 CharCount: 1
Code Point: 108
i: 3 CharCount: 1
Code Point: 108
i: 4 CharCount: 1
Code Point: 111
i: 5 CharCount: 1
Code Point: 97
i: 6 CharCount: 1
Code Point: 98
i: 7 CharCount: 1
Code Point: 33
i: 8 CharCount: 1
Code Point: 63
i: 9 CharCount: 1
Code Point: 49
i: 10 CharCount: 1
Code Point: 50
i: 11 CharCount: 1
Code Point: 68
i: 12 CharCount: 1
Code Point: 35
i: 13 CharCount: 1
Code Point: 97
i: 14 CharCount: 1
Code Point: 115
i: 15 CharCount: 1
Code Point: 68
i: 16 CharCount: 1
Code Point: 83
i: 17 CharCount: 1
Code Point: 70
i: 18 CharCount: 1
Code Point: 33
i: 19 CharCount: 1
Code Point: 38
i: 20 CharCount: 1
Code Point: 35
i: 21 CharCount: 1
Code Point: 36
i: 22 CharCount: 1
false
Code Point: 20320
i: 1 CharCount: 1
true

展开阅读全文
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值