public static void main(String[] args) {
String s= "this is a sample string.";
String searchString = "sample";
int count = 0;
// 检查子字符串出现的次数
while (s.contains(searchString)) {
count++;
// 移除已经检查过的子字符串
s= s.substring(s.indexOf(searchString) + searchString.length());
}
System.out.println("The string '" + searchString + "' occurs " + count + " times.");
}
Java判断字符串包含几个特定字符串
最新推荐文章于 2024-07-29 14:33:48 发布