The question can be divided into two points:

  1. Is there a method that returns true if the character is found in the string / TextView and accordingly false otherwise?

  2. If not, how can you check this condition in if without creating a separate method, since IMHO to create a method that will be called once is not very aesthetically pleasing.

  • one
    myTextView.getText (). toString.contains (something) - user3841429

1 answer 1

Yes, in the class String there is such a method:

 public boolean contains(CharSequence s) 

where s is an object of the class that implements the CharSequence interface.

UPD :

You can check for the presence of the character char c in the String str like this:

 boolean contains = str.contains(String.valueOf(c)); 

or so:

 boolean contains = str.indexOf(c) != -1 ? true : false; 

In the first case, char c converted to the String type and the String.contains(...) method is String.contains(...) ; in the second, the String.indexOf(...) method String.indexOf(...) for the character position of the char c in the string str if the specified character in the string no, the method returns -1 , if there is, its position in the string.

  • More specifically, for example, if you need to check for the presence of char c in the String str string, what will the method call look like? - Evgeniy
  • @Evgeniy, Updated the answer. - post_zeew