2014-02-25 36 views
4

我完全意识到字符串是不可变的,不能改变,可以是“editabile” - 哦争议!所以我试图让没有用于java中字符串的replace()方法,以实现字符串中特定字符与另一个字符交换的位置。我想尽可能简单地做到这一点,而不需要导入任何util或使用数组。到目前为止,我已经得到它来改变字符,但它没有正确返回,或者,那是...字符串结束。Java - 递归替换字符串中的字母

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); 
} 
+2

使用StringBuilder,因为它就是它的用途。这样做没什么奇怪的,事实上它比递归更清洁简单。 –

+0

你想修改现有的字符串还是从现有的字符串创建一个新的字符串? – Kakarot

+0

在你的return语句中,你最终创建了一个不符合你提到的要求的String实例。 – Kakarot

回答

-1

是的,气垫船满是鳗鱼是正确的。 StringBuilder是你的朋友。它是可变的,你可以将你的字符串提供给StringBuilder,然后做交换并在最后调用toString()并完成。

0

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

2

您可以通过使用“的charAt”方法用于字符串的把适当的字符转换成一个StringBuffer,然后运行“的toString”关于StringBuffer的

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(); 

} 

}

+0

使用StringBuilder +1。我个人喜欢非递归版本,因为它更干净,更易于理解。 – tonga

+0

@tonga - 它使用在多线程执行中很有用的StringBuffer – unnamed

5

如何工作的这这个罢工吗?有趣的尾巴递归。

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? 
+0

这是最好的方法。 – Chandru

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; 
} 

* 输出为*新的替换字符串是=>的hello world,I/M/J/V /程序控制/ M,如何/重新您TOD/Y?