API实现
new StringBuilder(str).reverse()
public class Test {
public static void main(String[] args) {
String str = "ABCDE";
System.out.println(reverseString(str));
}
private static String reverseString(String str) {
if(str.length() > 0 && str !=null){
return new StringBuilder(str).reverse().toString();
}
return str;
}
}
代码实现
创建char[],保存倒序的每个字母
public class Test {
public static void main(String[] args) {
String str = "ABCDE";
System.out.println(reverseString(str));
}
private static String reverseString(String str) {
if(str.length() > 0 && str !=null){
int len = str.length();
char[] chars = new char[len];
for (int i=len-1;i>=0;i--){
chars[len-1-i] = str.charAt(i);
}
return new String(chars);
}
return str;
}
}