【发布时间】:2018-05-27 07:17:23
【问题描述】:
如何在最后的递归中制作char 或示例:
removeChar ("hello world, let's go!",'l') → "heo word, et's go!llll"
removeChar("you should not go",'o') → "yu shuld nt goooo"
public static String removeChar(String word, char charToRemove) {
String char_toremove = Character.toString(charToRemove);
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == charToRemove) {
String newWord = word.substring(0, i) + word.substring(i + 1);
return removeChar(newWord, charToRemove);
}
}
System.out.println(word);
return word;
}
【问题讨论】:
-
不能用replace吗?
标签: java string recursion char