5 examples to reverse a string
There are many ways to reverse a string in java, this article will show you 5 examples how to reverse a string.
1)By the reverse function of StringBuffer or StringBuilder,
1 2 3 4 5 6 7 8 9 10 |
private String reverseString1(String str) { if (str == null) { return null; } return new StringBuffer(str).reverse().toString(); } public void test(){ System.out.println(reverseString1("abcd")); } |
output
1 |
dcba |
2)By charAt fucntion of String class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
private String reverseString2(String str) { if (str == null) { return null; } String result = ""; for (int i = str.length() - 1; i >= 0; i--) { result = result + str.charAt(i); } return result; } public void test(){ System.out.println(reverseString2("abcd")); } |
output
1 |
dcba |
3)By toCharArray function of String class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
private String reverseString3(String str) { if (str == null) { return null; } String result = ""; char[] chars = str.toCharArray(); for (int i = chars.length - 1; i >= 0; i--) { result = result + chars[i]; } return result; } public void test(){ System.out.println(reverseString3("abcd")); } |
output
1 |
dcba |
4)Use the feature of stack
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
private String reverseString4(String str) { if (str == null) { return null; } int stackSize = str.length(); Stack theStack = new Stack(); for (int i = 0; i < stackSize; i++) { theStack.push(str.charAt(i)); } String result = ""; while (!theStack.isEmpty()) { char ch = (char) theStack.pop(); result = result + ch; } return result; } public void test(){ System.out.println(reverseString4("abcd")); } |
output
1 |
dcba |
5)Use dichotomy to reverse string array
1 2 3 4 5 6 7 8 9 10 11 12 13 |
private String reverse5(String str) { if (str == null) { return null; } char[] chars = str.toCharArray(); int n = chars.length - 1; for (int i = 0; i < chars.length / 2; i++) { char temp = chars[i]; chars[i] = chars[n - i]; chars[n - i] = temp; } return new String(chars); } |
output
1 |
dcba |
which one is the most efficient
It is easy to get there ideas to reverse a string, but which one is the most efficient,let’ have a test.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
public static void main(String[] args) { // 实例一个长度为100000的字符串 String test = ""; Random random = new Random(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < 100000; i++) { sb.append(random.nextInt(10)); } test = sb.toString(); // reverseString1 long start1 = System.nanoTime(); String reverse1 = reverseString1(test); System.out.println("reverse1 time = " + (System.nanoTime() - start1)); // reverseString2 long start2 = System.nanoTime(); String reverse2 = reverseString2(test); System.out.println("reverse2 time = " + (System.nanoTime() - start2)); // reverseString3 long start3 = System.nanoTime(); String reverse3 = reverseString3(test); System.out.println("reverse3 time = " + (System.nanoTime() - start3)); // reverseString4 long start4 = System.nanoTime(); String reverse4 = reverseString4(test); System.out.println("reverse4 time = " + (System.nanoTime() - start4)); // reverseString5 long start5 = System.nanoTime(); String reverse5 = reverseString5(test); System.out.println("reverse5 time = " + (System.nanoTime() - start5)); System.out.println(test); System.out.println(reverse1); System.out.println(reverse2); System.out.println(reverse3); System.out.println(reverse4); System.out.println(reverse5); } |
the result output as below
1 2 3 4 5 |
reverse1 time = 3206954 reverse2 time = 5384374368 reverse3 time = 1996738321 reverse4 time = 1471417533 reverse5 time = 1476173 |
from the result we can find that the time consuming relationship of the five methods is: reverseString2>reverseString3>reverseString4>reverseString1>reverString5.
As the java office document said: The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.
so the reverseString2 is slower than reverseString3. The principle ofreverseString3 and reverseString4 is also by char concat, so they are slower than reverseString1. and the charAt efficient is slower than get char from array by index,so reverseString2>reverseString3>reverseString4>reverseString1. Next,check the StringBuffer source code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
public AbstractStringBuilder reverse() { boolean hasSurrogates = false; int n = count - 1; for (int j = (n-1) >> 1; j >= 0; j--) { int k = n - j; char cj = value[j]; char ck = value[k]; value[j] = ck; value[k] = cj; if (Character.isSurrogate(cj) || Character.isSurrogate(ck)) { hasSurrogates = true; } } if (hasSurrogates) { reverseAllValidSurrogatePairs(); } return this; } /** Outlined helper method for reverse() */ private void reverseAllValidSurrogatePairs() { for (int i = 0; i < count - 1; i++) { char c2 = value[i]; if (Character.isLowSurrogate(c2)) { char c1 = value[i + 1]; if (Character.isHighSurrogate(c1)) { value[i++] = c1; value[i] = c2; } } } } |
we can find that the StringBuffer reverse string by char array,and the recycle time is more than reverseString5,so reverseString1>reverseString5. The result is reverseString2>reverseString3>reverseString4>reverseString1>reverString5, reverString5 is the most efficient.