I might use String#matches() here, with a regex alternation:
CopyString input = "cpr1210";
if (input.matches("(?i).*(?:cpr|cvr|nr).*")) {
System.out.println("MATCH");
}
If you really wanted to use String#contains(), then you would need to have three separate calls to that method, for each sequence.
Videos
I might use String#matches() here, with a regex alternation:
CopyString input = "cpr1210";
if (input.matches("(?i).*(?:cpr|cvr|nr).*")) {
System.out.println("MATCH");
}
If you really wanted to use String#contains(), then you would need to have three separate calls to that method, for each sequence.
As mentioned in my comment to your question, you may use String#contains().
Copy String testString = "cor1210";
// will not print yes, because none of the sequences fit
if (testString.contains("cpr") || testString.contains("cvr") || testString.contains("nr")) {
System.out.println("yes");
}
Make sure your logic-syntax in the if-clause is correct.
I propose the following, null-safe alternative:
Optional.ofNullable(fullString)
.orElse("")
.contains(testStr);
Ideone.com demo
When fullStr is in fact null then "" will be used, and .contains(...) will almost always1 evaluate to false.
If we want to always return false when fullStr is null, we can use:
Optional.ofNullable(fullString)
.map(s -> s.contains(testStr))
.orElse(false);
Ideone.com demo
Note that this solution will return false when both fullString and testStr are null. I know that testStr is guaranteed to be != null, but thought it might be worth mentioning.
1It will evaluate to true when testStr is "" as well
fullstring.contains(testStr)
To avoid NullPointerExceptions, use an if statement and && operator:
if (fullstring != null && fullstring.contains(testStr)) {
// your logic
}
If fullstring is null, the second part won't execute.
Edit: I thought null was falsy.