/**
* 统计字符串中特定字符的个数
*/
public class Test02 {
public static void main(String[] args) {
String string = "fgsfadfegrgdsfafegsfdfdsafegesgd";
System.out.println(getCount(string, "fe"));
}
private static int getCount(String str,String key){
int count = 0;
int index = 0;
while((index = str.indexOf(key)) != -1){
index = index + key.length();
str = str.substring(index);
count ++;
}
return count;
}
}
第二道题
/**
* 截取最大字符串
*/
public class Test03 {
public static void main(String[] args) {
String s1= "fefeagrewgshellofafadfew";
String s2 = "sdahelloda";
String max=getMaxString(s1,s2);
System.out.println(max);
}
private static String getMaxString(String s1, String s2) {
for(int x = 0;x < s2.length(); x++){
for (int y = 0,z = s2.length() - x; z != s2.length() +1 ; y++,z++) {
String temp = s2.substring(y,z);
System.out.println(temp);
if(s1.contains(temp)){
return temp;
}
}
System.out.println("-------------------------------");
}
return "";
}
}