Hit enter after type your search item

Reverse a string in Java

You may use a number of ways for reversing the strings in Java programs. If you are working with mutable strings by using StringBuilder or StringBuffer class then these classes have a built-in reverse method.

toCharArray reverse example Reverse by split example

If using strings based on String class, it does have a built-in method for reversing a string. In that case, you may create new objects and reverse a string by writing your own code.

In the following section, I will show you using both i.e. by built-in methods of StringBuffer and StringBuilder class along with writing a small program for reversing a string, so keep reading.

An example of string reversing by StringBuilder Class

This can be the simplest way of reversing a string. Especially, if you are working with mutable strings and your program uses StringBuilder based strings then using its reverse method is handy.

See the following program for using the reverse method:

The Output:

Before SB reverse: String Reverse Demo

After SB reverse() method: omeD esreveR gnirtS

So, simply using the:


will reverse the order of sequence in the specified string.

With StringBuffer class

The StringBuffer class has the same method and you may reverse a string just like in above example. Just change the class name from StringBuilder to StringBuffer and you are done. See this program:

The Output:



Learning more about the difference between StringBuffer and StringBuilder.

No, I use String class – how to reverse it?

Well, you have to do a little work in that case for reversing a string. One of the solutions can be using the charAt with string length method.

The Java charAt() method returns the character in the specified string for the given index number. The index of a string starts at zero.

So, the solution is to get the specified string that needs to be reversed character by character and concatenate to a new string object from the last to the first character.

See the code below where a user is asked to enter a string (by using Scanner class).

As I entered different sentences to be reversed, the following is the output:

Enter a String to reverse:

So how exactly it worked?

In the for loop, the variable is assigned the value of string length -1, as string index starts at 0. The loop should keep on executing until it reaches the value greater than zero. In each iteration, the variable value is decremented by -1.

In the body of the loop, the charAt method is used that returns the character in the input string for the current value of the variable (that acts as the index position). The returned character is concatenated to a new String object that was initially declared as an empty string. That is it!

Reversing by toCharArray method of String class

You may also use the toCharArray method of the String class that converts a string to the new character array.

So, the string that you want to reverse is first converted to a character array and then you may use this solution to get a reversed string.

See online demo and code

Java reverse string

Using the split method of String class to reverse a string

Well, this is even more interesting solution. In that way, by little tweaking the code, you may reverse the given string letter by letter or word by word and even by sentences.

By the way, the String class split method is used to break the specified string by the given regex and returns an array of broken string.

See this example where the given string is reversed by using the split method:

See online demo and code

Java reverse string split

Reversing by words using split method

In above example, the string is reversed character by character. Only changing the regex to space for this example and you will see the returned result is a reversed word by word.

The Output:

Source string: Spit for reversing

The reversed string after split: reversing for Spit

Notice this line in the for loop body:

strRev = strRev + arrStr[i] + ” “;

Space is also concatenated in each iteration, otherwise, the words are attached to each other without space.  For example, this would have the result if no space was used in concatenation:

The reversed string after split: reversingforSpit

Reversing a paragraph – sentence by sentence using split method

Similarly, you may use the split method for reversing sentences in paragraphs. So, if you have long strings that contain paragraphs and you need to reverse by sentences rather than letters or words, then you may use the \\. (dot) regex.

The output of above code is:

Source string:  Spit for reversing. Reverse the sentences in the paragraphs. For that, use the dot regex. Taken?

The reversed string after split:  Taken?. For that, use the dot regex. Reverse the sentences in the paragraphs. Spit for reversing.

In that case, you need to make sure that source paragraph’s sentences are separated by dots (full stop). Or you may tweak the code by adding more regex like if paragraphs contain question marks or other characters at the end of the string.

For question marks, you may use the following regex in the split method:


I am sure, you will also find out how to add the question mark in the reversed string as well!

Using toString method with reverse method of StringBuilder class

Well, sounds back to the first two solutions? Yes, a little bit. But in that case, the resultant reversed string is still the mutable String object.

In that case, the reverse method of StringBuilder is used along with toString method. See the demo below:

The output:

After toString and reverse: ti esrever


This div height required for enabling the sticky sidebar