1.java编程将从键盘输入文本中的子字符串“word”替换为字符串“world”,并删除所有的子字符串“this”

如题所述

public static void main(String[] args) {
System.out.println("请输入字符串:");
Scanner in=new Scanner(System.in);
String s = in.nextLine();
System.out.println("替换word为world");
s = s.replace("word", "world");
System.out.println(s);
System.out.println("替换this");
s = s.replace("this", "");
System.out.println(s);
}

直接用String的replace函数就可以实现这是最简单的方法。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-01-05
1)ctrl+f 打开查找替换对话框
2) find -->“word” ;replace with-->“world” 点击replace all 按钮就可以全部替换
3)find -->“this” ;replace with-->为空格 点击replace all 按钮就可以全部删除本回答被网友采纳
第2个回答  2011-01-05
public static void main(String[] args){
System.out.println("键盘输入字符串:");
Scanner scan=new Scanner(System.in);
String s=scan.nextLine();
System.out.println("s="+newStr(s));
}
public static String newStr(String s){
int n=s.indexOf("word");
if(n>=0){
String sub=s.substring(n, n+4);
sub="world";
s=s.substring(0,n)+sub+s.substring(n+4,s.length());
}
int t=s.indexOf("this");
if(t>=0){
s=s.substring(0,t)+s.substring(t+4,s.length());
}
while(s.indexOf("word")>=0){
s=newStr(s);
}
return s;
}
第3个回答  2011-01-06
你把 ’this’替换成 ’’。就可以了,
也可以用 this=this.replace('this','')
相似回答