Easy way to reverse String

后端 未结 12 2519
北荒
北荒 2021-02-13 03:47

Without going through the char sequence is there any way to reverse String in Java

12条回答
  •  长发绾君心
    2021-02-13 04:23

    Using minimal API support. A simple algorithm.

    static String reverse(String str) {
        char[] buffer = str.toCharArray();
        for (int i = 0; i < buffer.length/2; ++i){
            char c = buffer[i];
            buffer[i] = buffer[buffer.length-1-i];
            buffer[buffer.length-1-i] = c;
        }
        return new String(buffer);
    }
    

提交回复
热议问题