思路1: 将字符串通过.split()切割成数组,然后遍历数组倒叙打印即可
思路2: 使用字符串缓冲类StringBuffer中的.reverse()方法实现字符串反转
具体代码如下:
方法一:
/**
* 如何将字符串反转
* @author hf
*
*/
public class Blogs1 {
public static void main(String[] args) {
String str = "abcdefg";
System.out.println("原字符串为:"+str);
String[] arr = str.split(""); //切割字符串为数组
System.out.print("反转的结果:");
for (int i = str.length()-1; i >= 0; i--) {
System.out.print(arr[i]);
}
System.out.println();
}
}
方法二:
/**
*
* @author hf
*
*/
public class Blogs1_2 {
public static void main(String[] args) {
StringBuffer sBuffer = new StringBuffer();
String str = "abcdefg";
System.out.println("原字符串为:"+str);
sBuffer.append(str);
System.out.println("反转字符串的结果:"+sBuffer.reverse());
}
}