String inversion in Java
Method of First push:
Public void convertStr (String str ){
// Convert a String object to a variable StringBuffer Class Object
// Call the reverse () method of the StringBuffer class to implement inversion.
String strReverse = new StringBuffer (str). reverse (). toString ();
System. out. println (strReverse );
}
Other methods:
Public void convertStr (String str ){
For (int I = str. length ()-1; I> = 0; I --)
{
// Output one character in reverse order each time
System. out. print (str. charAt (I ));
}
}
Method that is not desirable: troublesome
Public void convertStr (String str ){
String strNew = "";
String [] s = new String [str. length ()];
For (int x = 0; x <str. length (); x ++)
{
S [x] = str. substring (x, x + 1 );
}
For (int x = str. length ()-1; x> = 0; x --)
{
StrNew + = s [x];
}
System. out. println (strNew );
}
Author "loading"