import java.util.Scanner;
//循环录入字符串没有写,只有方法
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入一个字符串");
String s1 = sc.nextLine();
System.out.println("输入另一个字符串");
String s2 = sc.nextLine();
Object[] results = deleteSubString(s1, s2);
System.out.println("处理后的字符串为" + results[0]);
System.out.println("删除的个数为" + results[1]);
}
public static Object[] deleteSubString(String str1, String str2) {
int count = 0;
while(true) {
int indexOfSubString = str1.indexOf(str2);
if(indexOfSubString != -1) {
count++;
String s1 = str1.substring(0, indexOfSubString);
String s2 = str1.substring(indexOfSubString + str2.length());
str1 = s1 + s2;
}else {
break;
}
}
return new Object[]{str1, count};
}
} |