将一个字符串进行反转
https://www.cnblogs.com/binye-typing/p/9260994.html
package com.interview.algorithm;
public class StringReverse {
// StringBuffer
public static String reverse1(String str) {
return new StringBuilder(str).reverse().toString();
}
// toCharArray 逆向
public static String reverse2(String str) {
char[] chars = str.toCharArray();
String reverse = "";
for (int i = chars.length - 1; i >= 0; i--) {
reverse += chars[i];
}
return reverse;
}
// charAt 正向
public static String reverse3(String str) {
String reverse = "";
int length = str.length();
for (int i = 0; i < length; i++) {
reverse = str.charAt(i) + reverse;
}
return reverse;
}
public static void main(String[] args) {
String s = "cf_wu";
System.out.println(reverse1(s));
System.out.println(reverse2(s));
System.out.println(reverse3(s));
}
}