public static void main(String[] args) {
String str = "Hello World abc Hello";
test1 t = new test1();
int count = t.WorldCount(str, "Hello");
System.out.println(count);
}
public int WorldCount(String article, String word) {
// 1、先把文章打散成数组
String[] words = article.split(" ");
int res = 0;
for (int i = 0; i < words.length; i++) {
if (words[i].equals(word)) {
res++;
}
}
return res;
}
忽略大小写的比较
public int WorldCount(String article, String word) {
// 1、先把文章打散成数组
String[] words = article.split(" ");
int res = 0;
for (int i = 0; i < words.length; i++) {
// equalsIgnoreCase:忽略大小写的比较内容
if (words[i].equalsIgnoreCase(word)) {
res++;
}
}
return res;
}