java中怎么确定一个字符串在另一个字符串中出现的次数

2024-12-02 05:38:29
推荐回答(4个)
回答1:

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);

回答2:

string.tochararray方法将字符串的每个字符存入数组。
遍历数组查询出现的个数

回答3:

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;i if(charArray[i]==charArray2[0]){
h++;
}
}
System.out.println(h);

回答4:

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;
}