String str1 = "nihaoksdoksad ";
String str2 = "ok ";
int total = 0;
for (String tmp = str1; tmp != null&&tmp.length()> =str2.length();){
if(tmp.indexOf(str2) == 0){
total ++;
}
tmp = tmp.substring(1);
}
System.out.println(str1+ "中含有 "+total+ "个 "+str2);
string.tochararray方法将字符串的每个字符存入数组。
遍历数组查询出现的个数
String b="I wish you become better and better";
String d="e";
int h=0;
char[] charArray2 = d.toCharArray();
char[] charArray = b.toCharArray();
for(int i=0;iif(charArray[i]==charArray2[0]){
h++;
}
}
System.out.println(h);
package com.string.to;
public class Four {
public static void main(String args[]){
String t="a";
String s="advancea";
System.out.println(new Four().sort(t,s));
}
public int sort(String t,String s){
int count=0;
String[] k=s.split(t);
if(s.lastIndexOf(t)==(s.length()-t.length()))
count=k.length;
else
count=k.length-1;
if(count==0)
System.out.println("t do not founded in s");
else
return count;
return -1;
}