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.