java去除标点符号递归方法

标签 java recursion methods call

我做了一个方法来从字符串中删除一些标点符号,但它只是返回参数中传递的单词以及包含的标点符号,有人能发现哪里出了问题吗?

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/

相关文章:

java - Spring Boot Oauth2 授权服务器不接受有效 token

java - 如何使用 Spring Data JPA 保存具有手动分配标识符的实体?

c# - 将嵌套类转换为字典

java - 从 ArrayList 中删除对象

java - 如何将 RadioGroup 验证为必填字段?

java - 在 hibernate 查询的连接中返回的对象类型是什么

python - 在另一个 python 类中使用未绑定(bind)的方法

java - 如何使 for 循环从方法中输出数组的正确值

python - 在 Python 中实现快速排序

c++ - 所有带有 "recursion"的 child 的静态变量