Sam
Sam

Reputation: 121

how to start checking String from back if contains a specific letter

How to find a . from back to front in string

String calc = "abcd+abcd.abcd+abcd-abcd.abcd"

for (int i = calc.length(); i > 0; i--) {
    if (calc.charAt(i) == '.') {
      //Do something
    }
}

Upvotes: 2

Views: 68

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201437

Almost correct, I believe you want -

// for (int i = calc.length(); i > 0; i--) {
for (int i = calc.length() - 1; i >= 0; i--) { // <-- 0 is the first character.

Upvotes: 3

Related Questions