反转字符串

package com.test;

public class Test {
	
	public static void main(String[] args) {
		String str = "abcde";
		char[] chars = str.toCharArray();
		int length = chars.length;
		for(int i = 0; i < length/2; i++) {
			//交换位置
			char temp = chars[i];
			chars[i] = chars[length - 1 - i];
			chars[length - 1 - i] = temp;
		}
		String newStr = new String(chars);
		System.out.println(newStr);
	}

}

 

你可能感兴趣的:(java,字符串反转,算法总结)