有一个字符串,比如说是“Hello World, Welcome”,将该字符串的中World字串去掉之后的字符串打印出来。打印出来的字符串结果应该是:"Hello , Welcome"而且原始字符串和待查找的字符串,都是通过窗口输入的!

解决方案 »

  1.   

    String src="Hello World, Welcome!";
    String toReplaced="World";
    System.out.println(src.replaceAll(roReplaced,""));
      

  2.   


    public class strchange(s1,s2){
        String str = "";
        str = s1.replaceAll(s2,"");
        return str;
    }
      

  3.   

    import java.io.*;public class RemoveStr {
    public static void main(String args[]){
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String src = new String();
    String fnd = new String();

    System.out.print("source string: ");
    try{
    src = br.readLine();
    }catch(IOException e){
    e.printStackTrace();
    }

    System.out.print("to find: ");
    try{
    fnd = br.readLine();
    }catch(IOException e){
    e.printStackTrace();
    }

    System.out.println(src.replace(fnd,""));

    }
    }
      

  4.   

    楼上的那一位:最后一句应该改成System.out.println(src.replaceAll(fnd,""));
      

  5.   

    public class BinaryIODemo {
    public static void main(String[] args) throws IOException{
      System.out.println(args[0].replaceAll(args[1],""));
    }
    }
    java BinaryIODemo "hello world,welcome" "world"
      

  6.   

    楼上的够简单,连IOException都抛了
      

  7.   

    ^_^, sor 把那个 Throws IOException 去掉!  我改其他程序的时候忘了去了 ! 呵呵 !
      

  8.   

    我的最最拙劣的版本,哈哈,就学了Absolute Java的前两章写出来的。import java.io.*;
    public class _2
    {
    static final String CHANGE_TO = "love";
    public static void main(String[] args) throws IOException
    {
    BufferedReader bigzhu = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("输入原始字符串");
    String firstString = bigzhu.readLine();
    System.out.println("输入要改变的字符串:");
    String secondString = bigzhu.readLine();
    System.out.println("要改变成:");
            String changeTo = bigzhu.readLine();
    int suffix = firstString.indexOf(secondString);
    int changeStringNum = secondString.length();
    if (suffix == -1)
    {
    System.out.println("找不到要替换的字符串");
    System.exit(0);
    } else 
    if (suffix ==0)
    {
    System.out.println(changeTo + firstString.substring(suffix + changeStringNum));
    System.exit(0);
    }

    String frontString = firstString.substring(0,suffix);
    String backString = firstString.substring(suffix + changeStringNum);
    System.out.println(frontString + changeTo + backString);
    }
    }
      

  9.   

    怎么都不喜欢处理异常啊~~~
    都在main里面抛出去了~~~~:(
    这可不是什么好习惯啊~`不要教坏新人啊