Reputation: 21
In SQL, we have pattern matching operator like
with "_
" and "%
" to search for a string, or a part of a string.
Is there anything similar to that in Java.
There is the matches()
method. However, that only returns true
if the entire String
matches the regex.
I need a method that can evaluate if a given String
exists in another String
as a part of it.
Upvotes: 0
Views: 2727
Reputation: 1
You can use String.Contains()
Example:
public static void main(String[] args)
{
String s = "Hello World";
System.out.print(s.contains("Wor")); //return true
System.out.print(s.contains("wor")); //return false beacuse is case-sensitive
}
Upvotes: 0
Reputation: 469
contains is not usable if you need to perform regex matching.
You can trick the matches() method to behave as you expect, just saying that you want whatever sequence of character at start and end of the string.
Nice solution is to use matcher.find() which looks for occurrences of the regex. Look at class Matcher.
Upvotes: 1
Reputation: 1462
Check the contains method of the String class:
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#contains(java.lang.CharSequence)
Upvotes: 2