Java - 递归替换字符串中的字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22003104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Recursion to replace letter in string
提问by user2958542
I am full aware that strings are immutable and can't be changed and can be "editabile" - ohhh the controversy! So I am trying to get it so that withoutthe replace() method for strings in java, to implement where a specific char in a string gets switched out with another char. I want to do this as simply as possibly without needing to import any util or use arrays. thus far, I've gotten it to change the character, but it's not returning correctly, or, that is... the string ends.
我完全知道字符串是不可变的,不能改变,可以“编辑” - 哦,争议!所以我试图得到它,以便在java中没有字符串的replace()方法,以实现字符串中的特定字符被另一个字符切换的位置。我想尽可能简单地执行此操作,而无需导入任何实用程序或使用数组。到目前为止,我已经让它改变了字符,但它没有正确返回,或者,就是......字符串结束。
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);
}
回答by GenericJam
Yes, Hovercraft Full of Eels is correct. StringBuilder is your friend. It is mutable, you can just feed your String into StringBuilder and then do your swapping and call toString() at the end and your done.
是的,充满鳗鱼的气垫船是正确的。StringBuilder 是你的朋友。它是可变的,您可以将 String 输入 StringBuilder,然后进行交换并在最后调用 toString() 并完成。
回答by steven0529
You can use the Java predefined class StringBuilder. It is more effective than concatenating and manipulating Strings directly. StringBuilder also has a replace() method to cater your problem. See documentation StringBuilder#replace
您可以使用 Java 预定义类 StringBuilder。它比直接连接和操作字符串更有效。StringBuilder 也有一个 replace() 方法来解决您的问题。请参阅文档StringBuilder#replace
回答by ErstwhileIII
You can work this by using the "charAt" method for String an putting appropriate characters into a StringBuffer and then running "toString" on the StringBuffer
您可以通过使用 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();
}
}
}
回答by unigeek
How does this strike you? Fun with tail recursion.
这对你有什么影响?尾递归的乐趣。
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);
}
}
}
Output:
输出:
C:\>java Demo
hello world, i /m / j/v/ progr/m, how /re you tod/y?
回答by Rishi Dwivedi
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;
}
*Output is *New Replace String is => hello world, i /m / j/v/ progr/m, how /re you tod/y?
*输出是 *新替换字符串是 => hello world, i /m / j/v/ progr/m, how /re you tod/y?