How to Reverse a String in Java with Substring: Easy Guide

Robert Jul 13, 2026

Imagine you're working on a Java project and you're tasked with reversing a string. You might think, "That's simple, I can just write a quick loop and I'm done." But what if you want a more concise, elegant solution? That's where the substring method comes in. Let's dive into reversing a string in Java using the substring method.

Stop Struggling with String Manipulation
Stop Struggling with String Manipulation

Before we start, let's quickly recall how the substring method works. The substring method in Java takes two arguments: the starting index and the ending index. It returns a new string that's a substring of the original one, starting from the specified beginning index and ending at the specified end index (exclusive).

Java  Program to reverse every word in a String
Java Program to reverse every word in a String

Understanding String Reversal Using Substring

The core idea behind reversing a string using the substring method is to keep slicing off the first character and appending it to a new string until the original string is empty.

How to Reverse A String in JavaScript
How to Reverse A String in JavaScript

In Java, strings are actually immutable. This means that any operation on a string, including concatenation, creates a new string. So, while this might seem inefficient, it's actually quite performant because the substrings are stored in a constant amount of space.

Basic Approach

String Methods in Java Explained | Java Programming Basics
String Methods in Java Explained | Java Programming Basics

The simplest way to reverse a string using substring is to create a loop that iterates over the string in reverse order. At each step, you take the first character, concatenate it to the reversed string, and then slice off the first character from the original string.

Here's a simple implementation of this approach:

```java public class ReverseString { public static String reverse(String s) { String reversed = ""; for (int i = s.length() - 1; i >= 0; i--) { reversed += s.substring(i, i + 1); } return reversed; } } ```

Improving Performance

Java Strings - Learn the Essential Methods with its Syntax! - TechVidvan
Java Strings - Learn the Essential Methods with its Syntax! - TechVidvan

While the above approach works, it has a time complexity of O(n^2) due to the string concatenation inside the loop. A more efficient approach is to use a StringBuilder, which is designed for efficient string concatenation. Here's how you can do it:

```java public class ReverseString { public static String reverse(String s) { StringBuilder sb = new StringBuilder(); for (int i = s.length() - 1; i >= 0; i--) { sb.append(s.charAt(i)); } return sb.toString(); } } ```

In this version, we're appending individual characters to a StringBuilder, which is much more efficient than concatenating strings.

The Recursive Approach

Java Strings Cheat Sheet ☕ | Master String Problems for Coding Interviews
Java Strings Cheat Sheet ☕ | Master String Problems for Coding Interviews

If you enjoy the challenge of solving problems recursively, string reversal is a great candidate. The recursive approach also uses the substring method, but instead of looping, it calls itself with the reduced problem until the base case is reached.

The base case here is an empty string, which is ready to be reversed. Here's how you can implement this:

How to get first and last character of String in Java - Example
How to get first and last character of String in Java - Example
How to Reverse a String in C++, Python, and JavaScript
How to Reverse a String in C++, Python, and JavaScript
Strings in Java | Difference Between String, StringBuffer & StringBuilder Explained
Strings in Java | Difference Between String, StringBuffer & StringBuilder Explained
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Multithreading in Java Explained | Thread Life Cycle & Creation Methods
Multithreading in Java Explained | Thread Life Cycle & Creation Methods
Java Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually
Java script string method
Java script string method
How to Reverse A String in Python?
How to Reverse A String in Python?
String concatenation using StringBuilder class
String concatenation using StringBuilder class
Java Bootcamp London & UK – Part Time Java Developer Program
Java Bootcamp London & UK – Part Time Java Developer Program
a screen with the text 2 reverse an array description description description description description description description description description description description description description description description description description description description description description description description
a screen with the text 2 reverse an array description description description description description description description description description description description description description description description description description description description description description description description
Java String equals() Method
Java String equals() Method
Python Remove Substring with Examples
Python Remove Substring with Examples
Understanding Thread Life Cycle, Thread States in Java | Tutorial with Examples
Understanding Thread Life Cycle, Thread States in Java | Tutorial with Examples
[Solved] How to count Vowels and Consonants in Java String Word? Example
[Solved] How to count Vowels and Consonants in Java String Word? Example
How to Reverse Numbers in Java, C, C#?
How to Reverse Numbers in Java, C, C#?
How to make a twisted cord
How to make a twisted cord
What is String deduplication in Java? How to Save Memory from Duplicate String in Java 8? [Answer]
What is String deduplication in Java? How to Save Memory from Duplicate String in Java 8? [Answer]
Methods Of String Class In Java With Example
Methods Of String Class In Java With Example

```java public class ReverseString { public static String reverse(String s) { if (s.length() == 0) { return s; } else { return reverse(s.substring(1)) + s.charAt(0); } } } ```

Again, this approach has a time complexity of O(n^2) due to the string concatenation, so it's not the most efficient, but it's a great exercise in understanding recursion.

In the world of programming, there's often more than one way to solve a problem. Understanding these different methods can make you a more versatile coder. So, the next time you need to reverse a string, try out these substring-based methods and see which one fits your needs best.