用正则表达式[\u4e00-\u9fa5] public static void main(String[] args) { int count=0; String regEx = "[\\u4e00-\\u9fa5]"; //System.out.println(regEx); String str = "中文fdas"; //System.out.println(str); Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); while (m.find()) { for (int i = 0; i <= m.groupCount(); i++) { count=count+1; } } System.out.println("共有"+count+"个"); }
不知道有没有现成的,下面的方法可以参考一下。 public int numOfChineseChar(String sourStr) throws UnsupportedEncodingException{ int len = sourStr.length(); byte[] bytStr = sourStr.getBytes(); int intStr, count = 0;
public static void main(String[] args) {
int count=0;
String regEx = "[\\u4e00-\\u9fa5]";
//System.out.println(regEx);
String str = "中文fdas";
//System.out.println(str);
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
while (m.find()) {
for (int i = 0; i <= m.groupCount(); i++) {
count=count+1;
}
}
System.out.println("共有"+count+"个");
}
public int numOfChineseChar(String sourStr) throws UnsupportedEncodingException{
int len = sourStr.length();
byte[] bytStr = sourStr.getBytes();
int intStr, count = 0;
for(int i = 0; i < bytStr.length; i++){
intStr = bytStr[i];
if(intStr < 0){//JAVA中中文字符是负的BYTE值。
count++;
i++; //中文字符是双字节。
}
} return count;
}
//System.out.println(regEx);
String str = "中文fda她jjs";
String tem= str.replaceAll(regEx,"aa");
System.out.println(""+(tem.length()-str.length()));
不好意思,修改一下楼上的程序.
不知道有没有判断连续出现多少个中文的方法,
比如:"你好1233",连续出现2个中文.
而"你1233好",连续出现1个中文.
这个怎么办啊.有人帮帮我吗?
谢谢楼上所有的朋友!
char a;
int count=0;
//public String str="";
public void SetStr(String str){
//this.str=str;
for(int i=0;i<str.length();i++){
a=str.charAt(i);
if((a!='a')&&(a!='b')&&(a!='c')&&(a!='d')
&&(a!='e')&&(a!='f')&&(a!='g')&&(a!='h')
&&(a!='i')&&(a!='j')&&(a!='k')&&(a!='l')
&&(a!='m')&&(a!='n')&&(a!='o')&&(a!='p')
&&(a!='q')&&(a!='r')&&(a!='s')&&(a!='t')
&&(a!='u')&&(a!='v')&&(a!='w')&&(a!='x')
&&(a!='y')&&(a!='z')&&(a!='1')&&(a!='2')
&&(a!='3')&&(a!='4')&&(a!='5')&&(a!='6')
&&(a!='7')&&(a!='8')&&(a!='9')&&(a!='0')
&&(a!=',')&&(a!='.')&&(a!='/')&&(a!=';')
&&(a!=' ')&&(a!='[')&&(a!=']')&&(a!='\\')){
count++;
continue;
}
}
System.out.println("COUNT的值为:"+count);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Zw z= new Zw();
z.SetStr("zhongwen中文"); }}
你可以通过字母,数字和特殊字符的编码进行判断,我那些都忘了,只好写成这样了,如果是大小写字母的话在String类中 有不区分大小的方法 哎好久不干这个了都忘光了
然后将负数的byte值的个数除以2就可以得到。
int count = 0;
String regEx = "[\u4e00-\u9fa5]+[^\u4e00-\u9fa5]";
String str = "ss中文文fdas文";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
String repStr = null;
while (m.find()) {
repStr = m.group();
}
String regEx1 = "[\u4e00-\u9fa5]";
Pattern p1 = Pattern.compile(regEx1);
Matcher m1 = p1.matcher(repStr);
while (m1.find()) {
count = count + 1;
}
System.out.println("连续出现" + count + "个中文");
}
虽然程序写得很垃圾,但是达到要求了。
import java.util.*;public class CountNum{
private static String str = "中文语言fd何as最近几天dafds" +
"工作dfds效率不高fdsf番dfsfdsa啊";
private static Pattern p;
private static Matcher m;
//private static int count=0;
public static int getNum(String str){
int count=0;
String regEx = "[\\u4e00-\\u9fa5]";
p = Pattern.compile(regEx);
m = p.matcher(str);
while(m.find())count++;
return count;
}
public static int aloneChar(String str){
int together=0;
Map map=charLinked(str);
Iterator iter=map.values().iterator();
while(iter.hasNext()){
together+=((Integer)iter.next()).intValue();
}
return getNum(str)-together;
}
public static Map charLinked(String str){
Map map=new HashMap();
String temp=null;
String regEx = "[\\u4e00-\\u9fa5][\\u4e00-\\u9fa5]+";
p = Pattern.compile(regEx);
m = p.matcher(str);
while(m.find()){
temp=m.group();
map.put(temp,new Integer(temp.getBytes().length/2));
}
return map;
}
public static void main(String[] args) {
System.out.println(getNum(str));
System.out.println(charLinked(str));
System.out.println(aloneChar(str));
}
}
String str = "中文f1d3a她jjs";
String[] tem= str.split(regEx);
下面是测试结果,剩下就是你自己处理了。
//String str = "ssss中文f1d3a她jjs"-----[, 中文, 她]
//String str = "中文f1d3a她jjs"-------[中文, 她]
{
String input = "中文语言fd何as最近几天dafds工作dfds效率不高fdsf番dfsfdsa啊";
String temp = null;
Pattern p = Pattern.compile("[\u4E00-\u9FA5]+");
Matcher m = p.matcher(input);
while (m.find())
{
temp = m.group(0);
System.out.println(temp + ":" + temp.length());
}
}