java字符串匹配,另种方式对比
时间:2010-10-16 来源:sail out to sea
public class Checkb {
public static void main(String[] args){
String check = new String("dfAdfAdfwdfA15df6");
String match = new String("A");
int j = 0;
for(int i = 0;i<check.length();){
int m = check.indexOf(match, i);
if(m>=0){
i = i+m+1;
j++;
}
else break;
}
System.out.println("匹配次数为:"+(j));
}
}
//此方法先将字符串转化成数组在一一匹配,比较麻烦
public class Checka {
public static void main(String[] args){
String ck = new String("dfAdfAdfwdfA15df6");
String match = new String("f6");
int sm = 0;//小写字母计数
int bm = 0;//大写字母计数
int fc = 0;//非字符数结果
int k=0;//匹配计数
char ca[] = ck.toCharArray();
char mt[] = match.toCharArray();
//计数
for(int i = 0;i<ck.length();i++){
if(ca[i]>='a'&&ca[i]<='z') sm = sm+1;
if(ca[i]>='A'&&ca[i]<='Z') bm = bm+1;
else fc = i+1 - (sm+bm);
}
//实现匹配
for(int m = 0;m<ca.length;m++){
if(ca[m]==mt[0]){
int n = 0;
int j =m;
for(;n<mt.length;n++){
if(ca[j]== mt[n]) j++;
else break;
}
if(n==mt.length) k=k+1;
}
}
System.out.println("大写字母个数"+bm+"小写字母个数"+sm+"非字符个数"+fc);
System.out.println("匹配个数"+k);
}
}