【问题标题】:How to find string in another string and also ignoring lower / uppercase? [duplicate]如何在另一个字符串中查找字符串并忽略小写/大写? [复制]
【发布时间】:2020-05-07 03:43:35
【问题描述】:

我需要定义我的查询是 DDL 还是 DML。为此,我需要尝试在另一个字符串(我的查询)中找出部分字符串,例如“create”“update”等。

我的字符串:

"CreATE table test (id number);"
"sElECT * from user;"
"ALTER table;"

如果字符串包含“create/select/alter”并且忽略大小写,谁能告诉我如何返回 true?
我试过了:

Pattern.compile(Pattern.quote(query), Pattern.CASE_INSENSITIVE).matcher("CREATE").find())

但它有时有效,有时无效,我认为这是实现我的目标的坏方法。

【问题讨论】:

  • 这能回答你的问题吗? String contains - ignore case
  • 应该是Pattern.compile(Pattern.quote("create"), Pattern.CASE_INSENSITIVE).matcher(query).find())
  • 如果你想检查它是否包含任何创建/选择/更改 - Stream.of("create", "select" ,"alter").anyMatch(myString.toLowerCase()::contains);
  • 哇,事实上,我有它应该是相反的方式。谢谢 J Anand 老板:D

标签: java


【解决方案1】:

只需使用 String 类方法

"CreATE table test (id number);".toLowerCase().contains("create")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-21
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 2012-12-10
    • 2011-09-28
    相关资源
    最近更新 更多