-1

I have a string that will be different every time you run the code. I want to make an if statement that compares one char, to every individual character of the string. For example, if I were to compare 'c' to cat, it will return that there is a 'c'. BUT If I compared it to orange, it would return that there is not any 'c'. I can't figure out how to do this because the string will change length every time so I cannot simply do c = a && b && c because there could be a d.

Kedar Mhaswade
  • 4,535
  • 2
  • 25
  • 34
ricemvm
  • 7
  • 2

1 Answers1

-1

There is String#contains:

return "orange".contains("c");

If you want to do other kind of comparisons (such as that all characters in the String are "c"), you can loop over the characters:

for (char c : theString.toCharArray()){
  // do something for `c`
}

Or look at regular expressions.

Thilo
  • 257,207
  • 101
  • 511
  • 656