Do you know any strings `s` that give different results from `s.trim()` compared to `s.replaceAll("^\\s", "").replaceAll("\\s$", "")` ? I think `trim()` has a fairly efficient implementation, if we can live with that.