0

I need to define is my query is DDL or DML. To do this i need to try to find out part of strings like "create" "update" etc. in another string (my query).

My strings:

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

Can someone tell me how to return true if string contains "create / select / alter" and also ignore lower and uppercase?
I tried that:

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

but it sometimes works and sometimes not, i think this is bad way to achieve my goal.

Brarord
  • 611
  • 5
  • 18
  • 2
    Does this answer your question? [String contains - ignore case](https://stackoverflow.com/questions/14018478/string-contains-ignore-case) – tobsob Jan 21 '20 at 06:05
  • 2
    Should be `Pattern.compile(Pattern.quote("create"), Pattern.CASE_INSENSITIVE).matcher(query).find())` – J Anand Boss Jan 21 '20 at 06:07
  • 1
    If you want check that it contains any of the create / select / alter - `Stream.of("create", "select" ,"alter").anyMatch(myString.toLowerCase()::contains);` – Saurav Kumar Singh Jan 21 '20 at 06:13
  • Wow, in fact, I had it the opposite way it should be. Thanks J Anand Boss :D – Brarord Jan 21 '20 at 06:14

1 Answers1

2

Just use String class methods

"CreATE table test (id number);".toLowerCase().contains("create")
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64