Quick problem: reverse a String

Solution for strings that don’t contain surrogate keys etc. (UTF issues):

public String reverse(final String s) {
    final char[] chars = s.toCharArray();
    final int length = chars.length;
    for (int i = 0; i < length / 2; i++) {
        final char temp = chars[i];
        chars[i] = chars[length - i - 1];
        chars[length - i - 1] = temp;
    }

    return new String(chars);
}
Advertisements