RosettaCodeData/Task/Strip-whitespace-from-a-string-Top-and-tail/Java/strip-whitespace-from-a-string-top-and-tail-8.java
2023-07-01 13:44:08 -04:00

19 lines
650 B
Java

public static String ltrim(String s) {
int offset = 0;
while (offset < s.length()) {
int codePoint = s.codePointAt(offset);
if (!Character.isWhitespace(codePoint)) break;
offset += Character.charCount(codePoint);
}
return s.substring(offset);
}
public static String rtrim(String s) {
int offset = s.length();
while (offset > 0) {
int codePoint = s.codePointBefore(offset);
if (!Character.isWhitespace(codePoint)) break;
offset -= Character.charCount(codePoint);
}
return s.substring(0, offset);
}