Java reverse string example shows how to reverse a string. This example also shows how to reverse words as well as reverse the order of the individual words of the string in Java.
How to reverse a String in Java?
1) Using the StringBuilder or StringBuffer class
The String class does not provide any methods to reverse a string. However, the StringBuilder class does. Create a new StringBuilder object from the string, use the reverse
method of StringBuilder class to reverse the StringBuilder content, and then convert StringBuilder to String object using the toString
method.
1 |
public StringBuilder reverse() |
This method reverses the content of the StringBuilder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.javacodeexamples.stringexamples; public class StringReverseExample { public static void main(String[] args) { String str = "Hello World"; String reverseString = new StringBuffer(str).reverse().toString(); System.out.println( "Reversed String: " + reverseString); } } |
Output
1 |
Reversed String: dlroW olleH |
2) Using the charAt method of the String class (without using the reverse method)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
String str = "Hello World"; int strLength = str.length(); StringBuilder sb = new StringBuilder( strLength ); for( int i = strLength - 1; i >=0 ; i-- ){ //append the characters one by one to StringBuilder sb.append( str.charAt(i) ); } String reverseString = sb.toString(); System.out.println("Reversed String: " + reverseString); |
Output
1 |
Reversed String: dlroW olleH |
Note: This solution is not compatible with the Unicode characters as they are formed using a pair of characters. This solution reverses the pair and will corrupt the characters.
3) Using Apache Commons Library
If you are using the Apache Commons library, you can use the reverse
method of the StringUtils class to reverse a string.
1 2 3 4 |
String str = "Hello World"; String reverseString = StringUtils.reverse(str); System.out.println("Reversed String: " + reverseString); |
Output
1 |
Reversed String: dlroW olleH |
How to reverse the words of string in Java?
Suppose you want to reverse individual words of the string and not the entire string then the above given solutions will not work as they reverse the entire string. For example, if the input string is “Hello World”, the output should be “olleH dlroW” not “dlroW olleH”. Use below given solution instead.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
String str = "Hello World"; //split string by space to get the words String[] parts = str.split(" "); StringBuilder sb = new StringBuilder(str.length()); //loop through the individual words for( int i = 0; i < parts.length; i++ ) { //reverse the individual word sb.append( new StringBuilder(parts[i]).reverse() ); //append space, but we don't want to append space after the last word if( i != (parts.length - 1) ) sb.append(" "); } System.out.println("String with reverse words: " + sb.toString()); |
Output
1 |
String with reverse words: olleH dlroW |
How to reverse the order of the words in a string?
Use the below given solution if you want to reverse the order of the words in a string. For example, if the string contains “Java String Reverse”, then the output should be “Reverse String Java”. So basically, we are reversing the order of the words so that the first word comes last, the second word comes second last and so on.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
String str = "I Love Java"; //split the string by space to get the words String[] parts = str.split(" "); StringBuilder sb = new StringBuilder(str.length()); //loop through the words in reverse order for(int i = (parts.length - 1) ; i >= 0 ; i--){ //append individual words sb.append(parts[i]); //append space if it is not the last word if( i != 0 ) sb.append(" "); } System.out.println("Reverse order of the words: " + sb.toString()); |
Output
1 |
Reverse order of the words: Java I Love |
Note: The StringBuilder class is not available in Java JDK 1.4 and below. Use the StringBuffer class instead.
This example is a part of the Java String tutorial.
Please let me know your views in the comments section below.