Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java OR operator in if string contains statement

I have the following code.

for (String str5 : verticesposition2) {
    if(!str5.contains(("Vertex")||("Name")||("Transmittance")) {
        System.out.println(str5); 
    }                           
}

As you can see above if the string does NOT contain Vertex, Name or Transmittance I want it to print out. However Im getting a compilation error saying that the || operator is undefined for the argument types. I'm relatively new to programming so Im not sure what this means could someone kindly point in the right direction on how to fix my code?

like image 878
Anton James Avatar asked Sep 21 '26 19:09

Anton James


1 Answers

Java doesn't have a syntax like that, but you can put the "or" in a regex:

if (!str5.matches(".*(Vertex|Name|Transmittance).*")) {

Note that java's matches() (unlike many other languages) must match the whole string to return true, hence the .* at each end of the regex.

like image 80
Bohemian Avatar answered Sep 23 '26 08:09

Bohemian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!