Top community members

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

[Edit]
EN

Java - how to reverse a string

2 points
Created by:
AnnLen
1662

1. Overview

In this post we will take a look how to reverse a string in java. Of course there is more than one way to do it.

String example  = "ABCD"; // we have this string
String reversed = "DCBA"; // expected - reversed string

2. Reverse string with StringBuilder - the simplest way

Code example:

String example = "ABCD";
String reversed = new StringBuilder(example).reverse().toString();

System.out.println(example);  // ABCD
System.out.println(reversed); // DCBA

Output:

ABCD
DCBA

3. Reverse string iterative method

Code example:

public class JavaReverseStringExample2 {

    public static void main(String[] args) {
        String example = "ABCD";

        char[] arr = example.toCharArray();
        int j = arr.length - 1;

        for (int i = 0; i < arr.length / 2; i++, j--) {
            char tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
        String reversed = new String(arr);

        System.out.println(example);  // ABCD
        System.out.println(reversed); // DCBA
    }
}

Output:

ABCD
DCBA

4. Reverse string recursive method

Code example: 

public class JavaReverseStringRecursive {

    public static void main(String[] args) {
        String example = "ABCD";
        String reversed = reverseStringRecursive(example);

        System.out.println(example);  // ABCD
        System.out.println(reversed); // DCBA
    }

    public static String reverseStringRecursive(String str) {
        if ((null == str) || (str.length() <= 1)) {
            return str;
        }

        return reverseStringRecursive(str.substring(1)) + str.charAt(0);
    }
}

Output:

ABCD
DCBA

5. Reverse string with Apache commons

Code example:

import org.apache.commons.lang3.StringUtils;

public class JavaReverseStringApacheCommons {

    public static void main(String[] args) {
        String example = "ABCD";
        String reversed = StringUtils.reverse(example);

        System.out.println(example);  // ABCD
        System.out.println(reversed); // DCBA
    }
}

Output:

ABCD
DCBA

NOTE:

How Apache commons StringUtils.reverse looks internally:

public class StringUtils {

    // ..

    public static String reverse(final String str) {
        if (str == null) {
            return null;
        }
        return new StringBuilder(str).reverse().toString();
    }

    // ..

}

 

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more