码迷,mamicode.com
首页 > 编程语言 > 详细

Java实现给定字符串的倒序输出

时间:2016-07-01 15:59:34      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

1.除2判中法:

public String orderDesc(String str){
       byte [] bytes = str.getBytes();
       for ( int i = 0; i < bytes.length / 2 ; i++) {
             Byte b = bytes [i] ;
             bytes [i] = bytes [bytes.length - 1 -i ] ;
             bytes [bytes.length - 1 -i ] = b ;
       }
       return new String (bytes) ;
}

2.String类的toCharArray();

public static String orderDesc(String str){
      char[] charArray = str.toCharArray(); 
      String newStr = "";
      for (int i=charArray.length-1; i>=0; i--){ 
        newStr += charArray[i]; 
      } 
      return newStr;
}

 

Java实现给定字符串的倒序输出

标签:

原文地址:http://www.cnblogs.com/lxcmyf/p/5633150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!