本文實例講述了java統(tǒng)計漢字字數(shù)的方法。分享給大家供大家參考,具體如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class TongJiHanZi { public static int count(String text) { String Reg= "^[\u4e00-\u9fa5]{1}$" ; //正則 int result= 0 ; for ( int i= 0 ;i<text.length();i++){ String b=Character.toString(text.charAt(i)); if (b.matches(Reg))result++; } return result; } public static void main(String[] args) { String text = "漢字漢字仮名かなカナ 0189 azAZ ./ " ; System.out.println(count(text)); } } |
希望本文所述對大家java程序設計有所幫助。