Java

How to reverse a String in Java

To reverse a string means to have the string in the reverse order. For example, the string “abcde” can be reversed to “edcba”, and “pool” can be reversed to “loop”. As another example, the string “abc def ghi” can be reversed to “ihg fed cba”.

The string literal of an object for the String class is constant. This means that non of its characters can be changed or displaced. Fortunately, Java has two other string-like classes, called StringBuffer and StringBuilder. Each of these two classes has the reverse() method to reverse a string.

So, to reverse a string, convert the String object to a StringBuffer or StringBuilder object. Use the reverse() method of either of these classes to reverse the string. Then convert the result back to a string object.

To convert a String object to a StringBuffer object, use the string object in the StringBuffer constructor. To convert a StringBuffer object back to a String object, use the StringBuffer object in the String constructor.

To convert a String object to a StringBuilder object, use the string object in the StringBuilder constructor. To convert a StringBuilder object back to a String object, use the StringBuilder object in the String constructor.

String and StringBuffer Constructions

String to StringBuffer

The syntax to construct a StringBuffer object from a String object is:

where sb is the StringBuffer object. The following program illustrates this:

    public class TheClass {
        public static void main(String[] args) {
            String str = "Higher Level";
StringBuffer sb = new StringBuffer(str);
System.out.println(sb);
        }
    }

The output is:

Higher Level

Note that the program did not have to import the StringBuffer class.

StringBuffer to String

The syntax to construct a String object from a StringBuffer object is:

String str = new String(StringBuffer sb);

where str is the string object. The following program illustrates this:

    public class TheClass {
        public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Higher Level");
            String str = new String(sb);
System.out.println(str);
        }
    }

The output is:

Higher Level

String and StringBuilder Constructions

String to StringBuilder

The syntax to construct a StringBuilder object from a String object is:

StringBuilder sbl = new StringBuilder(String str);

where sbl is the StringBuilder object. The following program illustrates this:

    public class TheClass {
        public static void main(String[] args) {
            String str = "Higher Level";
            StringBuilder sbl = new StringBuilder(str);
System.out.println(sbl);
        }
    }

The output is:

Higher Level

Note that the program did not have to import the StringBuilder class.

StringBuilder to String

The syntax to construct a String object from a StringBuilder object is:

String str = new String(StringBuilder sbl);

where str is the string object. The following program illustrates this:

    public class TheClass {
        public static void main(String[] args) {
            StringBuilder sbl = new StringBuilder("Higher Level");
            String str = new String(sbl);
System.out.println(str);
        }
    }

The output is:

Higher Level

Reversing with StringBuffer

The procedure to reverse a string with StringBuffer is as follows:

StringBuffer sb = new StringBuffer(String str)

StringBuffersbr = sb.reverse()

            String strr  = new String(StringBuffersbr);

The following program illustrates this:

    public class TheClass {
        public static void main(String[] args) {
            String str = "Higher Level";
StringBuffer sb = new StringBuffer(str);
StringBuffersbr = sb.reverse();
            String strr  = new String(sbr);
System.out.println(strr);
        }
    }

The output is:

leveL rehgiH

Reversing with StringBuilder

The procedure to reverse a string with StringBuilder is as follows:

            StringBuilder sbl = new StringBuilder(String str)

StringBuilder  sblr = sbl.reverse()

            String strr  = new String( StringBuilder sblr);

The following program illustrates this:

    public class TheClass {
        public static void main(String[] args) {
            String str = "Higher Level";
            StringBuilder sbl = new StringBuilder(str);
StringBuilder  sblr = sbl.reverse();
            String strr  = new String(sblr);
System.out.println(strr);
        }
    }

The output is:

leveL rehgiH

StringBuffer and StringBuilder

StringBuilder is a simplified form of StringBuffer. If StringBuffer is used with multiple threads, there will be good synchronization. StringBuilder does not offer good synchronization with multiple threads. StringBuilder should be used only with single-thread programs.

The main methods for StringBuilder and StringBuffer are the append() and insert() methods. With these methods, their string literals can be increased in length and also modified. These objects cannot have the null value as one of their characters.

Reversing with String and Array

The string value (characters that form the string literal) for the String object is constant. The characters can, of course, be read. The sequence of characters is read-only. If the characters are read backward into an array and the array converted into a string object of the string class, that would be the string’s reversal.

Using this procedure, the size of the string has to be determined with the following statement:

len = str.length();

where length() is the string class method to return the length of the string. To make an array-of-chars from a string, use the following string construction:

String strR = new String(char[])

The following program uses this procedure to reverse a string in Java:

    public class TheClass {
        public static void main(String[] args) {
            String str = "Higher Level";
            int len = str.length();
char[] arr = new char[len];

            for (int i=len; i>0; i--)
arr[len-i] = str.charAt(i-1);

            String strR = new String(arr);
System.out.println(strR);
        }
    }

The output is:

leveL rehgiH

same as before. Note the use of the charAt() method to obtain the character at an index of the string object.

Conclusion

The string literal of an object for the String class is constant. This means that non of its characters can be changed or displaced. Fortunately, Java has two other string-like classes, called StringBuffer and StringBuilder. Each of these two classes has the reverse() method to reverse a string.

So, to reverse a string, convert the String object to a StringBuffer or StringBuilder object. Use the reverse() method of either of these classes to reverse the string. Then convert the result back to a string object.

To convert a String object to a StringBuffer object, use the string object in the StringBuffer constructor. To convert a StringBuffer object back to a String object, use the StringBuffer object in the String constructor.

To convert a String object to a StringBuilder object, use the string object in the StringBuilder constructor. To convert a StringBuilder object back to a String object, use the StringBuilder object in the String constructor.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.