Java - 用于替换字符串中的字母的递归

时间:2014-02-25 01:56:13

标签: java string

我完全意识到字符串是不可变的,不能改变,可以是“editabile” - 哦争议!所以我试图得到它,以便没有 java中的字符串的replace()方法,以实现字符串中的特定字符串与另一个字符串切换出来的情况。我希望尽可能简单地执行此操作,而无需导入任何util或使用数组。到目前为止,我已经让它改变了角色,但它没有正确地返回,或者,就是......字符串结束了。

public static void main(String[] args) {
    String words = "hello world, i am a java program, how are you today?";
    char from = 'a';
    char to = '/';

    replace(s, from, to);
}
public static String replace(String s, char from, char to){
    if (s.length() < 1)
        return s;
    if (s.charAt(0) == from) {
        s = to + s.substring(1);
    }
    System.out.println(s);
return s.charAt(0) + replace(s.substring(1, s.length()), from, to);
}

5 个答案:

答案 0 :(得分:6)

这对你有什么影响?有趣的尾递归。

public class Demo {

  public static void main(String[] args) {
    String words = "hello world, i am a java program, how are you today?";
    char from = 'a';
    char to = '/';

    System.out.println(replace(words, from, to));
  }

  public static String replace(String s, char from, char to){
    if (s.length() < 1) {
      return s;
    }
    else {
      char first = from == s.charAt(0) ? to : s.charAt(0);
      return first + replace(s.substring(1), from, to);
    }
  }

}

输出:

C:\>java Demo
hello world, i /m / j/v/ progr/m, how /re you tod/y?

答案 1 :(得分:2)

你可以使用String的“charAt”方法,将适当的字符放入StringBuffer,然后在StringBuffer上运行“toString”

public class Replacement {
public static void main(String[] args) {
    String words = "hello world, i am a java program, how are you today?";
    char from = 'a';
    char to = '/';

    String changed = replace(words, from, to);
    System.out.println(words);
    System.out.println(changed);
}

public static String replace(String s, char from, char to) {
    StringBuffer result = new StringBuffer(s.length());

    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == from) {
            result.append(to);
        } else {
            result.append(s.charAt(i));
        }
    }
    return result.toString();

}

}

答案 2 :(得分:2)

 Try this code work for u enjoy it
   public static void main(String[] args) {
    String words = "hello world, i am a java program, how are you today?";
    char from = 'a';
    char to = '/';

    //String ss = words.replace(from, to);
    System.out.println(words);
    String ss = replace(words, from, to);// recieveing String from replace()
    System.out.println("New Replace String is =>  "+ss );
    }

public static String replace(String s, char from, char to){
    if (s.length() < 1)
        return s;
         for(int i=0;i<s.length();i++){
        if (s.charAt(i) == from) {
            s = s.substring(0, i)+to + s.substring(++i);
            System.out.println(s);
            return replace(s, from, to);//calling replace()
        }
    }
    return s;
}

* 输出为* 新替换字符串=&gt;你好世界,i / m / j / v / progr / m,你是怎么回事?

答案 3 :(得分:0)

您可以使用Java预定义类StringBuilder。它比直接连接和操作字符串更有效。 StringBuilder还有一个replace()方法来解决您的问题。请参阅文档StringBuilder#replace

答案 4 :(得分:-1)

是的,充满鳗鱼的气垫船是正确的。 StringBuilder是你的朋友。它是可变的,您可以将String提供给StringBuilder,然后进行交换并在结束时调用toString()并完成。