A summary of common methods for realizing the reverse output of strings in Java _java

Source: Internet
Author: User
Tags stringbuffer

This article summarizes the Java implementation of the reverse string output of the common methods, shared for everyone to reference. The specific methods are as follows:

1. The most readily conceivable estimate is the use of ToCharArray () of the String class, and the method of outputting the array in reverse.

The implementation code is as follows:

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. The substring () method provided by the string class can also be used to output the reverse string in a recursive way.

The implementation code is as follows:

Import Javax.swing.JOptionPane; 
public class ReverseString {public 
  static void reversestring (String str) { 
    if (str.length () = 1) { 
      System.ou T.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. There is also a string that is not defined as a class, but is defined as a StringBuffer class, using the reverse () method in the StringBuffer class to directly reverse the string.

The implementation code is as follows:

Import Javax.swing.JOptionPane; 
public class ReverseString {public 
  static void reversestring (String str) { 
    StringBuffer stringbuffer = new String Buffer (str); 
     
    System.out.print (Stringbuffer.reverse ()); 
  } 
   
  public static void Main (string args[]) { 
    string originalstring; 
     
    originalstring = Joptionpane.showinputdialog ("Please input a String:"); 
     
    ReverseString (originalstring);     
  } 


I hope this article will help you with your study of Java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.