Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate the python operator "in" in Java?

Tags:

java

That's a very beautiful operator and it's really frustrating to don't have it in Java.

if ("Nova Categoria" in textField1.getText()) {
        textField1.setText("");
        textField1.setForeground(Color.BLACK);
    }

The bloody "==" does not give me what I want, and my Pythonic Mind can't give me the solution to this right now, because I want an "in".

How would you simulate an "in" operator in Java?

like image 930
Ericson Willians Avatar asked Jun 12 '26 01:06

Ericson Willians


2 Answers

if (textField1.getText().contains("Nova Categoria"))
{
    ...
}
like image 198
tianz Avatar answered Jun 14 '26 14:06

tianz


You can use contains method of String instead as follows:

if (textField1.getText().contains("Nova Categoria")) {
        textField1.setText("");
        textField1.setForeground(Color.BLACK);
    }

Look here String#contains to know more about this method.

like image 45
Vishal K Avatar answered Jun 14 '26 13:06

Vishal K



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!