小编Ma *_*Tâm的帖子

Java以递归方式删除格式中的内容

我使用递归来删除文本中不必要的字符.

EX:source ="这是demo java remove string"

regex ="这,是,java"

result ="demo remove string"

当程序停止递归函数时,我很难不提供条件.如果有任何方法可以更有效地使用递归工作,请告诉我

public class NewClass2 {

    static String regexs[] = {"This","is", "java"};

    private static String getRecursive(String source, int level) {

        String content = "";

        if (level > regexs.length - 1) {
            return "";
        }

        level++;

        for (String regex : regexs) {
            content = source.replaceAll(regex, "");

            if (!content.equals("")) {
                content = getRecursive(content, level);
            }
        }
        return content;
    }

    public static void main(String[] args) {
        String result = getRecursive("This is demo …
Run Code Online (Sandbox Code Playgroud)

java arrays string recursion

2
推荐指数
1
解决办法
149
查看次数

标签 统计

arrays ×1

java ×1

recursion ×1

string ×1