统计字符串在字符串中出现的个数以及判断是否存在某字符串,随时Java基础,但是值得记录
package com.example.xiaohong;
public class XiaoHong {
public static boolean catDog(String str){
if (str.contains("catdog")){
System.out.println("true");
return true;
}else {
System.out.println("false");
return false;
}
}
public static int countHi(String str){
int count = 0;
for (int i = 0; i < str.length(); i++) {
//相当于双层遍历
if (str.substring(i).startsWith("hi")){
count++;
}
}
System.out.println(count);
return count;
}
public static void main(String[] args) {
catDog("catdog");
countHi("hihi");
countHi("abc hi ho");
countHi("ABChi hi");
}
}