Starting with `input = input.trim()` would reduce the repetition and should below be `length() - 1`?