我做了一个方法来从字符串中删除一些标点符号,但它只是返回参数中传递的单词以及包含的标点符号,有人能发现哪里出了问题吗?
public static String removePunctuation(String word) {
if (word.charAt(0) >= 32 && word.charAt(0) <= 46) {
if(word.length() == 1){
return "";
} else {
return removePunctuation(word.substring(1));
}
} else {
if(word.length() == 1){
return "" + word.charAt(0);
} else {
return word.charAt(0) + removePunctuation(word.substring(1));
}
}
}//end method
最佳答案
我运行了您随输入提供的代码:
h.ello
并得到输出hello
我在这里看到了一个功能齐全的方法。我认为您尝试删除的标点符号不属于您在 if 语句中提供的 ASCII 范围。根据图表检查您的 ASCII 值。
ASCII values chart to compare with
如果输入不包含正确的值:
h[ello
将返回输出 h[ello
因为[
是 ASCII 值 91,超出了您提供的范围:
>= 32 && <= 46
关于java去除标点符号递归方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22774673/