package homework;
import java.util.*;
/*(1)從鍵盤循環(huán)錄入錄入一個字符串,輸入"end"表示結束
(2)定義一個方法
public Object[] deleteSubString(String str1,String str2) {
}
(3)方法功能描述:從str1中刪除所有的str2,并返回刪除后的結果,返回結果為Object[]數(shù)組
* 該數(shù)組的第一個元素為刪除所有的str2后的最終的字符串
* 該數(shù)組的第二個元素為刪除的str2的個數(shù)*/
public class Work4 {
public static void main(String[] args) {
//String str = end();
// System.out.println(str);
Object[] ob = deleteSubString("ab? cd cddccdefghcd","cd");
System.out.println(ob[0] +" "+ ob[1]);
}
//利用stringbuffer的append和indexof功能,當沒有索引的時候坝咐,indexof返回-1? 實現(xiàn)功能(1)
public static String end() {
StringBuffer str = new StringBuffer();
while (true) {
String a = new Scanner(System.in).next();
str.append(a);
if (str.indexOf("end") >= 0) {break;}
}
// System.out.println(str);
return str.toString();
}
/*方法功能描述:從str1中刪除所有的str2,并返回刪除后的結果,返回結果為Object[]數(shù)組
* 該數(shù)組的第一個元素為刪除所有的str2后的最終的字符串
* 該數(shù)組的第二個元素為刪除的str2的個數(shù)*/
public static Object[] deleteSubString(String str1,String str2) {
/*StringBuffer s1 = new StringBuffer(str1);*/
int index = 0;
int count = 0;
while((index=str1.indexOf(str2)) != -1) {
count++;
str1 = str1.substring(0, index)+str1.substring(index+str2.length());
}
Object[] ob ={str1,count};
return ob;
}
}