I saw an article a few days ago, saying that using Java can reverse a string in several ways. First of all to understand what is called the reversal of the string, that is, a string to come over, for example, "Upside down is the puppy" reversed is "dog small is read over". The next step is to record all the ways that you can think of.
1, the first idea is to use the inverse method of the string class directly, sorry, this is not possible, because the string class does not have this method. Well, make the array, and then iterate through the arrays, swapping the corresponding characters in the array in turn.
|
Direct use of array first swap
public string Reverse1 (String str) {
if (str = = NULL | | str.length () <= 1) {
return str;
}
char[] array = Str.tochararray ();
char[] Reversearray = new Char[array.length];
for (int i = 0; i < Array.Length; i++) {
Reversearray[array.length-1-I] = array[i];
}
return new String (Reversearray);
} |
2, in addition to the method used above, in fact, we can also use recursive iterative method, because if there is only one character, the reversal of a character is itself.
|
Use recursion in a smart way
public string Reverse2 (String str) {
if (str = = NULL | | str.length () <= 1) {
return str;
}
Return Reverse2 (str.substring (1)) + str.charat (0);
} |
3, the string class does not reverse the method of the string, but StringBuffer has this method, so you can first convert the string into StringBuffer, and then use StringBuffer inversion, The stringbuffer is then converted to string after the reversal. This trick may be the most convenient.
|
public string Reverse3 (String str) {
if (str = = NULL | | str.length () <= 1) {
return str;
}
StringBuffer sb = new StringBuffer (str);
SB = Sb.reverse ();
return sb.tostring ();
} |
4, in addition to the use of StringBuffer direct reversal method, we can also rely on stringbuffer other methods to complete .
|
This is the Setcharat () method using the StringBuffer
public string Reverse4 (String str) {
if (str = = NULL | | str.length () <= 1) {
return str;
}
StringBuffer sb = new StringBuffer (str);
for (int i = 0; I < (Str.length ()/2); i++) {
int swapindex = Str.length ()-1-i;
Char swap = Sb.charat (Swapindex);
Sb.setcharat (Swapindex, Sb.charat (i));
Sb.setcharat (i, swap);
}
return sb.tostring ();
}
This is the append () method using StringBuffer, which inserts the characters of the original string in reverse
public string Reverse4 (String str) {
if (str = = NULL | | str.length () <= 1) {
return str;
}
StringBuffer sb = new StringBuffer (Str.length ());
for (int i = Str.length ()-1; I >= 0; i--) {
Sb.append (Str.charat (i));
}
return sb.tostring ();
}
|
5, think again, the process of reversing the string, is to put the characters in a queue, and then in reverse order to take out, this happens to Java stack stacks like, advanced.
|
public string reverse5 (String str) {
if (str = = NULL | | str.length () <= 1) {
return str;
}
String result = "";
char[] arr = Str.tochararray ();
stack<character> stack = new stack<character> ();
for (char A:arr) {
Stack.push (a);
}
int length = Stack.size ();
Note that this place requires the length of the stack to be recorded, because the stack's pop method will change the stack size during traversal.
for (int i = 0; i < length; i++) {
Result + = Stack.pop ();
}
return result;
}
|
All roads lead to Rome, so long as we are willing to think and try, we will eventually find one or more ways to achieve our goal. Hope that in the future on the road of programming, can divergent thinking, to find ways to solve various problems.
650) this.width=650, "src=" http://images.cnitblog.com/blog/437282/201411/041405283617635.gif "width="/>
Moe's IT People
Java Inverse string