Several methods of Java implementing string reverse output

Source: Internet
Author: User

1. The easiest estimate is to use the ToCharArray () of the string class and then reverse the method of outputting the array.

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 classreversestring { Public Static voidMain (String args[]) {string originalstring; String resultstring=""; Originalstring= Joptionpane.showinputdialog ("Please input a String:"); Char[] Chararray =Originalstring.tochararray ();  for(inti=chararray.length-1; i>=0; i--) {resultstring+=Chararray[i]; } joptionpane.showmessagedialog (NULL, Resultstring,"Reverse String", Joptionpane.information_message); }  }

2. You can also use the substring () method provided by the string class to output an inverted string using a recursive approach.

import Javax.swing.JOptionPane;  Public classreversestring { Public Static voidreversestring (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 voidMain (String args[]) {string originalstring; Originalstring= Joptionpane.showinputdialog ("Please input a String:");           ReverseString (originalstring); }  } 
import Javax.swing.JOptionPane;  Public classreversestring { Public Static voidreversestring (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 voidMain (String args[]) {string originalstring; Originalstring= Joptionpane.showinputdialog ("Please input a String:");           ReverseString (originalstring); }  } 

3. What's more, instead of defining strings as String classes, define them as a StringBuffer class, using the reverse () method in the StringBuffer class to reverse the string directly.

import Javax.swing.JOptionPane;  Public classreversestring { Public Static voidreversestring (String str) {StringBuffer StringBuffer=NewStringBuffer (str); System. out. Print (Stringbuffer.reverse ()); }                    Public Static voidMain (String args[]) {string originalstring; Originalstring= Joptionpane.showinputdialog ("Please input a String:");                 ReverseString (originalstring); }      } 

Several methods of Java implementing string reverse output

Related Article

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.