java自带替换
String s="hlz_and_hourui哈哈";
String new_S=s.replaceAll("哈", "笑毛");
System.out.println(new_S);
则输出为:"hlz_and_hourui笑毛笑毛";
package find_repalce_keywords; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; public class find_repalce_keywords { public static void main(String[] args) { String s="hlz_and_hourui哈哈";
//java自带替换
String new_S=s.replaceAll("哈", "笑毛");
System.out.println(new_S);
String input = null;
String finding=null;
System.out.println("enter a string:");
System.out.flush(); try {
input=getString();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("输入你想查找的词");
try {
finding=getString();
} catch (IOException e) {
e.printStackTrace();
}
FindAndRepace far=new FindAndRepace();
far.repalce(input, finding); } //I/O操作
public static String getString() throws IOException {
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String s=br.readLine();
return s;
}
} class FindAndRepace
{
int i=0;
public int repalce(String str,String finding)
{
if(str.indexOf(finding, i)!=-1)
{ System.out.print("要找的"+finding+"在"+(str.indexOf(finding, i)+1)+"||||");
i=str.indexOf(finding, i)+1;
repalce(str, finding);//递归
}
else
{
System.out.println("搜索结束");
}
return i;
} }
运行效果: