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.
Asked
Active
Viewed 414 times
-1

Kedar Mhaswade
- 4,535
- 2
- 25
- 34

ricemvm
- 7
- 2
-
Could you please show codes ? – Suresh Atta May 04 '16 at 01: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. – Bethany Louise May 04 '16 at 02:01
1 Answers
-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