1、用StringBuffer:
public static String reverse1(String str){
return new StringBuffer(str).reverse().toString();
}
StringBuffer在内存使用上比String好
StringBuffer s = new StringBuffer(“abc”);
//初始化出的StringBuffer对象的内容就是字符串”abc”
String s = “apple”;
StringBuffer sb = new StringBuffer(s);
String s1 =sb.toString();
2、传统字符数组方法
public static String reverse2(String str){
char[] array = str.toCharArray();
String res = "";
for(int i = str.length() - 1; i >= 0; i--){
res += array[i];
}
return res;
}