*"与&QUOT Java的递归字符串比较;作为一个通配符 [英] Java Recursive String Comparison with "*" as a Wildcard

查看:242
本文介绍了*"与&QUOT Java的递归字符串比较;作为一个通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写检查字符串中的每个字母来比较它们的递归方法。我在做任何的*字符匹配的麻烦,并在需要时充当尽可能多的字母。 (使它成为一个通配符)

I'm writing a recursive method that checks each letter of the string to compare them. I'm having trouble making the "*" character match with any, and act as as many letters as needed. (Making it a wildcard)

我在想,如果有人可以给我一个提示上,将使用的算法?

I was wondering if someone can give me a hint on the algorithm that would be used?

下面是我到目前为止所。

Here is what I have so far.

public static boolean match(String x, String y) {
    return match_loop(x, y, 0, 1);
}

public static boolean match_loop(String a, String b, int i, int s) {
    try {
        if (a == b) {
            return true;
        }

        if (i >= a.length() && i >= b.length()) {
            return true;
        }

        if (a.charAt(i) == b.charAt(i)) {
            return match_loop(a, b, i + 1, s);
        }


        //(((...A bunch of if statements for my other recursion requirements

        return false;

    } catch (java.lang.StringIndexOutOfBoundsException e) {
        return false;
    }
}

public static void main(String[] args) {
    System.out.println(match("test", "t*t")); // should return true
}

我在想什么做的是增加另一个实际参数的的方法,一个int,将作为一个字母backcounter。基本上,我在想这个 如果a或b的字符(是)(S原本是1)是*,回忆起与S + 1的递归。 然后几个不同的IFS语句来修正错误。然而,这种方法似乎很长的和重复的。是否有任何其他的算法,我可以使用?

What I was thinking of doing is adding another arguement to the method, an int that will act as a letter backcounter. Basically I'm thinking of this if a or b at char(i-s) (s originally being 1.) is a *, recall the recursion with s+1. and then a few more different ifs statements to fix the bugs. However this method seems really long and repetitive. Are there any other algorithms I can use?

推荐答案

不要使用 == 字符串值比较。使用等于()方法。

Do not use == for String value comparison. Use the equals() method.

若(a ==二)如果a.equals(B)

这篇关于*"与&QUOT Java的递归字符串比较;作为一个通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆