How to Reverse a String in Java

Reverse a string in Java is a quite easy task.

I’ll provide you 6 pieces of Java code to reverse a string.

I think  StringBuilder.reverse() and  StringBuffer.reverse() are the best string reverse functions in Java.

It’s already optimized, looks simple and easy.

But sometimes you have a task to implement string reversal using for loop, recursion or even reverse a string word by word.

Java Reverse String Methods Overview

So you have a task: you need to write a Java program to reverse a string.

To be more precise – you need to write a code to reverse letters in a string.

I mentioned above about  StringBuilder.reverse() and  StringBuffer.reverse().

First of all, the difference is StringBuffer is threadsafe and StringBuilder is not.

If you take one of these reverse methods it would be a good choice.

It’s a source code of String Builder reverse method:

Looks a little bit complicated, but you shouldn’t care about it, it’s a problem of JDK developers.

The most important for you is that this method is well-optimized and tested.

I prepared code examples in the next sections. Let’s take a look.

Tweet Reverse Methods OverviewClick To Tweet

String Builder Reverse

Easy, fast and well-tested way to reverse a string, but not threadsafe.

Example:

Output:

Tweet String Builder MethodClick To Tweet

String Buffer Reverse

Easy, well-tested, threadsafe, but slower than StringBuilder.reverse().

Example:

Output:

Tweet String Buffer MethodClick To Tweet

String Utils Reverse

Apache Commons StringUtils is the most popular library to process strings.

It provides fancier API but uses  StringBuilder.reverse()  method inside.

To include this library you should add maven dependency to your pom.xml:

and use StringUtils reverse method.

Example:

Output:

Looks better but in general the same as a StringBuilder.

Tweet String Utils MethodClick To Tweet

Reverse a String in a For Loop

The solution is simple:

  1. Get a char array from the source string.
  2. Create a target array to store chars.
  3. Iterate through source char array and store letters in target array in reverse order.
  4. Create a new string from the target array.

Example:

Or you can switch reversed array of chars to StringBuilder.

Example:

Or even without retrieving char array.

Example:

Output:

Tweet For Loop MethodClick To Tweet

Reverse a String Using Recursion

The reverse function looks easy:

For example, you want to reverse “Hello” string.

The method will be executed 6 times:

Output:

Tweet Recursion MethodClick To Tweet

Reverse Word Letters in a String

The task is to keep words in its places but reverse characters in each word.

The plan is the following:

  1. Split source string by whitespace to an array
  2. Reverse each string in an array
  3. Join an array of strings back to one string

The example is in Java 8:

Output:

Tweet Word by Word MethodClick To Tweet

Reverse Words in String

The task is to reverse words in a string but letters in each word should not be changed.

Steps:

  1. Split by whitespace to list
  2. Reverse list
  3. Join back to string

The source code is in Java 8, but if someone needs I can provide an example without streams.

Example:

The same behavior has  StringUtils.reverseDelimited(s, c) method.

Example:

Output:

Feel free to ask any questions.

Tags:
Scroll Up