-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.

2
  • Could you please show codes ? Commented May 4, 2016 at 1:57
  • @3kings: That would only return true if the first letter is a c. return false must be placed after the for loop, or else it will return false on the first letter if it is not a c. Commented May 4, 2016 at 2:01

1 Answer 1

-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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.