Search code examples
javavalidationstringcharacter

How can I check if a single character appears in a string?


In Java is there a way to check the condition:

"Does this single character appear at all in string x"

without using a loop?


Solution

  • You can use string.indexOf('a').

    If the char a is present in string :

    it returns the the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.