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 static void main(String[] args) {
String str = "Higher Level";
StringBuffer sb = new StringBuffer(str);
System.out.println(sb);
}
}
The output is:
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:
where str is the string object. The following program illustrates this:
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Higher Level");
String str = new String(sb);
System.out.println(str);
}
}
The output is:
String and StringBuilder Constructions
String to StringBuilder
The syntax to construct a StringBuilder object from a String object is:
where sbl is the StringBuilder object. The following program illustrates this:
The output is:
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:
where str is the string object. The following program illustrates this:
The output is:
Reversing with StringBuffer
The procedure to reverse a string with StringBuffer is as follows:
StringBuffersbr = sb.reverse()
String strr = new String(StringBuffersbr);
The following program illustrates this:
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:
Reversing with StringBuilder
The procedure to reverse a string with StringBuilder is as follows:
The following program illustrates this:
The output is:
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:
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:
The following program uses this procedure to reverse a string in Java:
The output is:
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.