再不见青山 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);
}