//将字符串转成数组,对元素进行倒序遍历,然后存入StringBuilder,再打印.
public class Demo08 {
public static void main(String[] args) {
String s = "abcdefg";
StringBuilder sb = new StringBuilder();
char[] ch = s.toCharArray();
for(int x = ch.length -1 ; x >= 0; x --)
{
sb.append(ch[x]);
}
System.out.println(sb.toString());
}
}
//第二种.直接转换,
public class Test8 {
public static void main(String[] args){
//StringBuffer作为一个容器可以对字符串内容进行修改
StringBuffer sb = new StringBuffer("abcde");
System.out.println(sb.reverse());
} |
|