黑马程序员技术交流社区

标题: 将字符串中进行反转。abcde --> edcba*/ [打印本页]

作者: wxm278187618    时间: 2016-11-6 00:29
标题: 将字符串中进行反转。abcde --> edcba*/

//将字符串转成数组,对元素进行倒序遍历,然后存入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());
        }       
作者: Jarc    时间: 2016-11-6 18:34
水的可以





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2