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

Java实现字符串倒序输出的几种方法

时间:2017-04-09 10:40:45      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:bar   mat   string类   length   ==   dialog   sage   format   --   

1. 最容易想到的估计就是利用String类的toCharArray(),再倒序输出数组的方法了。

 

import javax.swing.JOptionPane;  
  
public class ReverseString {  
      
    public static void main (String args[]){  
        String originalString;  
        String resultString = "";     
          
        originalString = JOptionPane.showInputDialog("Please input a String: ");  
          
        char[] charArray = originalString.toCharArray();  
      
        for (int i=charArray.length-1; i>=0; i--){  
            resultString += charArray[i];  
        }  
          
        JOptionPane.showMessageDialog(null, resultString, "Reverse String", JOptionPane.INFORMATION_MESSAGE);  
    }  
} 

 

 

 

import javax.swing.JOptionPane;  
  
public class ReverseString {  
      
    public static void main (String args[]){  
        String originalString;  
        String resultString = "";     
          
        originalString = JOptionPane.showInputDialog("Please input a String: ");  
          
        char[] charArray = originalString.toCharArray();  
      
        for (int i=charArray.length-1; i>=0; i--){  
            resultString += charArray[i];  
        }  
          
        JOptionPane.showMessageDialog(null, resultString, "Reverse String", JOptionPane.INFORMATION_MESSAGE);  
    }  
}

 

 

2. 还可以利用String类提供的subString()方法,利用递归的办法输出倒序字符串。

import javax.swing.JOptionPane;  
  
public class ReverseString {  
    public static void reverseString (String str){  
        if (str.length() == 1){  
            System.out.print(str);  
        }  
        else{  
            String subString1 = str.substring(0, str.length()-1);  
            String subString2 = str.substring(str.length()-1);  
              
            System.out.print(subString2);  
              
            reverseString (subString1);           
        }  
    }  
      
    public static void main (String args[]){  
        String originalString;  
          
        originalString = JOptionPane.showInputDialog("Please input a String: ");  
          
        reverseString (originalString);       
    }  
} 
import javax.swing.JOptionPane;  
  
public class ReverseString {  
    public static void reverseString (String str){  
        if (str.length() == 1){  
            System.out.print(str);  
        }  
        else{  
            String subString1 = str.substring(0, str.length()-1);  
            String subString2 = str.substring(str.length()-1);  
              
            System.out.print(subString2);  
              
            reverseString (subString1);           
        }  
    }  
      
    public static void main (String args[]){  
        String originalString;  
          
        originalString = JOptionPane.showInputDialog("Please input a String: ");  
          
        reverseString (originalString);       
    }  
} 

 

 

3. 还有就是不把字符串定义为String类,而是定义成一个StringBuffer类,用StringBuffer类中的reverse()方法直接倒序字符串。

 

    import javax.swing.JOptionPane;  
      
    public class ReverseString {  
        public static void reverseString (String str){  
            StringBuffer stringBuffer = new StringBuffer (str);  
             
            System.out.print(stringBuffer.reverse());  
        }  
         
        public static void main (String args[]){  
            String originalString;  
             
            originalString = JOptionPane.showInputDialog("Please input a String: ");  
             
            reverseString (originalString);         
        }  
    } 

 

Java实现字符串倒序输出的几种方法

标签:bar   mat   string类   length   ==   dialog   sage   format   --   

原文地址:http://www.cnblogs.com/QQ846300233/p/6683819.html

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