忽略字符串中的数字

cle*_*lem 1 java string

例子:

    String t;
    String j ="j2se";
    Scanner sc =new Scanner(System.in);
    System.out.println("enter the search key:");
    t=sc.next();
    if (j.contains(t))
    {
        System.out.println("yes");
    }
    else System.out.println("no");
Run Code Online (Sandbox Code Playgroud)

如果用户输入“j2”、“se”或“j2s”作为输入,则输出为“是”。如果输入为“jse”,则输出为“no”。

是否有一种方法可以忽略字符串搜索中存储的数字,例如忽略大写或小写字母?

Tho*_*duc 5

为什么你不只是创建这个字符串的副本,里面没有数字?在 juste 之后将这个新字符串与您的数组进行比较?

喜欢 :

String stringWithoutNumber = j.replaceAll("\\d+","");

stringWithoutNumber.contains(t);
Run Code Online (Sandbox Code Playgroud)

许多程序都有一个名为 sanitize 的函数,它在比较之前清理 url 或字符串。

希望有帮助

- 编辑 -

您不需要输入+\\d+因为方法 replaceAll 替换了所有数字。但它更有效,因为它减少了替换。