Reputation: 28
I want to find a function in java that can check if string contain pattern "%A%B%" just like 'LIKE' statement in SQL. This function will return true if the string contain the pattern and false if not.
Can anyone suggest any class, function or line of code? Thank you!
Upvotes: 1
Views: 136
Reputation: 19906
Regular expression. Learn more here: https://docs.oracle.com/javase/tutorial/essential/regex/
The easiest way of calling it is using String.matches(String regex)
If you want to check the same regular expression more often, it's better to precompile it and use a Pattern
.
A typical invocation sequence is then
Pattern p = Pattern.compile(".*A.*B.*"); // you keep this stored for re-use
Matcher m = p.matcher("BARBARIAN");
boolean b = m.matches();
There is a good Online Regex Tester and Debugger tool, where you can check your regular expression.
Upvotes: 1
Reputation: 140299
Pattern.compile(".*A.*B.*").matches(input)
will return true if input
contains an A followed by a B.
Upvotes: 1