再不见青山 2024-08-31 17:46:50

JAVA 反转字符串的最快方法,大概比StringBuffer.reverse()性能提升5倍

public static String reverseStr(String str) {
        char[] charArray = str.toCharArray();
        int left = 0;
        int right = charArray.length - 1;
        while (left < right) {
            char temp = charArray[left];
            charArray[left] = charArray[right];
            charArray[right] = temp;
            left++;
            right--;
        }
        return new String(charArray);
}

评论

Home - Wiki
Copyright © 2011-2024 iteam. Current version is 2.138.0. UTC+08:00, 2024-12-22 00:41
浙ICP备14020137号-1 $Map of visitor$