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

barfoon picture barfoon · Feb 3, 2009 · Viewed 945k times · Source

In Java is there a way to check the condition:

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

without using a loop?

Answer

mP. picture mP. · Feb 3, 2009

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.