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.

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).

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.

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

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

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

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:














![[Solved] How to count Vowels and Consonants in Java String Word? Example](https://i.pinimg.com/originals/9e/34/94/9e3494d47f9aeda7562f34f97430ad70.png)


![What is String deduplication in Java? How to Save Memory from Duplicate String in Java 8? [Answer]](https://i.pinimg.com/originals/38/84/9d/38849de06af10de703970433af2f95db.jpg)

```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.