This tutorial will show you string reverse example in Java. We have various ways to reverse a string in Java and I have presented here five ways to reverse a string. String class in Java is an immutable class and that’s why it does not have reverse() method to reverse the given string. We will use here StringBuilder and StringBuffer‘s reverse() method, we will build our own custom method, using recursion with substring() method and Java 8’s Lambda and Stream API to reverse the given string.

Prerequisites

Java 1.8

Example

We will see the different ways to reverse a given string in Java programming language.

StringBuffer’s reverse() Method

We will use StringBuffer‘s reverse() method to reverse the given string.

public String usingStringBufferReverse(final String string) {
	if (string == null || string.isEmpty()) {
		return string;
	}
	String reverseString = new StringBuffer(string).reverse().toString();
	return reverseString;
}

StringBuilder’s reverse() Method

We will use StringBuilder‘s reverse() method to reverse the given string.

public String usingStringBuilderReverse(final String string) {
	if (string == null || string.isEmpty()) {
		return string;
	}
	String reverseString = new StringBuffer(string).reverse().toString();
	return reverseString;
}

Writing Custom Method

We will write our own method in Java to reverse a given string.

public String usingCustomReverse(final String string) {
	if (string == null || string.isEmpty()) {
		return string;
	}
	String reverse = "";
	for (int i = string.length() - 1; i >= 0; i--) {
		reverse = reverse + string.charAt(i);
	}
	return reverse;
}

Using Recursion

We will use recursion using substring() method to reverse a given string.

public String usingRecursion(final String string) {
	if (string == null || string.isEmpty() || string.length() < 2) {
		return string;
	}
	return usingRecursion(string.substring(1)) + string.charAt(0);
}

Using Java 8’s Lambda & Stream API

We will use Java 8’s Lambda and Stream API to reverse a given string.

public String usingJava8LambdaStream(final String string) {
	return IntStream.range(0, string.length()).map(i -> string.charAt(string.length() - i - 1))
			.collect(StringBuilder::new, (s, c) -> s.append((char) c), StringBuilder::append).toString();
}

Creating Main Class

Create a Java main class to test our above methods to reverse a string.

package com.roytuts.stringreverse;
import java.util.stream.IntStream;
public class StringReverseExample {
	public static void main(String[] args) {
		StringReverseExample sr = new StringReverseExample();
		String word = "HelloWorld";
		// quick wasy to reverse String in Java - Use StringBuffer
		String reverse = sr.usingStringBufferReverse(word);
		System.out.printf(" original String : %s , reversed String : %s  %n", word, reverse);
		// another quick to reverse String in Java - use StringBuilder
		reverse = sr.usingStringBuilderReverse(word);
		System.out.printf(" original String : %s , reversed String : %s %n", word, reverse);
		// one way to reverse String using custom method
		reverse = sr.usingCustomReverse(word);
		System.out.printf(" original String : %s , reversed String : %s %n", word, reverse);
		// reverse String using recursion
		reverse = sr.usingRecursion(word);
		System.out.printf(" original String : %s , reversed String : %s %n", word, reverse);
		// reverse String using Java 8 IntSTream
		reverse = sr.usingJava8LambdaStream(word);
		System.out.printf(" original String : %s , reversed String : %s %n", word, reverse);
	}
	public String usingStringBufferReverse(final String string) {
		if (string == null || string.isEmpty()) {
			return string;
		}
		String reverseString = new StringBuffer(string).reverse().toString();
		return reverseString;
	}
	public String usingStringBuilderReverse(final String string) {
		if (string == null || string.isEmpty()) {
			return string;
		}
		String reverseString = new StringBuffer(string).reverse().toString();
		return reverseString;
	}
	public String usingCustomReverse(final String string) {
		if (string == null || string.isEmpty()) {
			return string;
		}
		String reverse = "";
		for (int i = string.length() - 1; i >= 0; i--) {
			reverse = reverse + string.charAt(i);
		}
		return reverse;
	}
	public String usingRecursion(final String string) {
		if (string == null || string.isEmpty() || string.length() < 2) {
			return string;
		}
		return usingRecursion(string.substring(1)) + string.charAt(0);
	}
	public String usingJava8LambdaStream(final String string) {
		return IntStream.range(0, string.length()).map(i -> string.charAt(string.length() - i - 1))
				.collect(StringBuilder::new, (s, c) -> s.append((char) c), StringBuilder::append).toString();
	}
}

Testing

Run the above main class, you will see below output into the console:

 original String : HelloWorld , reversed String : dlroWolleH
 original String : HelloWorld , reversed String : dlroWolleH
 original String : HelloWorld , reversed String : dlroWolleH
 original String : HelloWorld , reversed String : dlroWolleH
 original String : HelloWorld , reversed String : dlroWolleH 

Source Code

You can download source code.

Thanks for reading.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *