String字符串反转
思路:
先把对应的字符串 转换为 字符数组,遍历对应的字符数组,从后往前抓取对应的字符,放入到空的字符串变量。
public class Test01 {
public static void main(String[] args) {
String str = "abcdef";
System.out.println(reverse(str)); //fedcba
}
方式一:
public static String reverse(String str){
char[] charArray = str.toCharArray();
String reverse = "";
for (int i = charArray.length -1; i >= 0; i--) {
reverse += charArray[i];
}
return reverse;
}
方式二:
public static String reverse1(String str) {
return new StringBuilder(str).reverse().toString();
}
}