1. Overview

Working with Strings is a fundamental aspect of Java programming. StringBuilder is an often-used utility for String manipulations, such as concatenation, reversing, etc. Understanding how to convert between String and StringBuilder is essential for efficient String handling.

In this quick tutorial, we’ll explore how to perform these conversions effectively.

2. Introduction to the Problem

*In Java, Strings are immutable,* meaning once an object is created, it cannot be changed. On the other hand, StringBuilder is a mutable sequence of characters, which allows us to modify the contents without creating new objects.

Therefore, we often convert a String to a StringBuilder for manipulations and then convert the StringBuilder object back to a String to obtain the result.

We’ll first examine StringBuilder to String conversion and then the other way around. In the end, we’ll use the conversion techniques to solve a practical problem.

For simplicity, we’ll use unit test assertions to verify that we get the expected results.

3. Converting a StringBuilder to a String

Calling the StringBuilder.toString() method is the most straightforward way to convert a StringBuilder object to a String:

StringBuilder sb = new StringBuilder("Converting SB to String");
String result = sb.toString();
assertEquals("Converting SB to String", result);

The toString() method returns a String containing all characters in the StringBuilder object’s sequence in the same order.

However, sometimes we only convert a segment from the StringBuilder object’s sequence. In this case, we can use StringBuilder‘s substring() method:

result = sb.substring(11, 13);
assertEquals("SB", result);

In this example, *we pass the begin and end indexes to the substring() method to get the required part from the StringBuilder‘s character sequence.*

Next, let’s look at the opposite direction, how to convert String objects to a StringBuilder.

4. Converting Strings to a StringBuilder

To convert a String to a StringBuilder, we can simply pass the String object to StringBuilder‘s constructor:

String input = "Converting String to SB";
StringBuilder sb = new StringBuilder(input);
assertEquals(input, sb.toString());

Alternatively, we can first create an empty StringBuilder instance and then leverage the append() method to append our String value to the StringBuilder‘s character sequence:

String input = "C C++ C# Ruby Go Rust";
StringBuilder sb = new StringBuilder().append(input);
assertEquals(input, sb.toString());

When we aim to create a StringBuilder object from one single String, this may look a bit awkward compared to the constructor parameter approach. However, it can be handy to convert multiple String values to a StringBuilder object. Next, let’s see an example:

String[] strings = new String[] { "C ", "C++ ", "C# ", "Ruby ", "Go ", "Rust" };
StringBuilder sb2 = new StringBuilder();
for (String str : strings) {
    sb2.append(str);
}
assertEquals(input, sb2.toString());

As the above example shows, we want to convert an array of String values to a StringBuilder. So, after creating an empty StringBuilder, *we call append() in a loop to append all elements from the String array to the StringBuilder‘s sequence*.

5. A Practical Example: Concatenating and Reversing

Finally, let’s solve a practical problem using the conversion skills we’ve learned.

Let’s say we’re given three input String values. We need to perform some transformations and get a String result following these steps:

  • First, join the three String values using ” | “ as the separator
  • Reverse the join result

This is a typical String transformation problem that we can solve using StringBuilder. 

Next, let’s see how it gets solved:

String str1 = "c b a";
String str2 = "^_*";
String str3 = "z y x";
 
StringBuilder sb = new StringBuilder(str1)
  .append(" | ")
  .append(str2)
  .append(" | ")
  .append(str3)
  .reverse();
String result = sb.toString(); 
assertEquals("x y z | *_^ | a b c", result);

The above code creates a StringBuilder with str1 using the constructor, appends the separators and other Strings using append(), and then calls reverse() to reverse the character sequence.

Finally, sb.toString() converts the StringBuilder back to a String.

5. Conclusion

In this article, we’ve explored how to convert between String and StringBuilder. By understanding these conversions, we can efficiently manipulate Strings in our Java programs.

As always, the complete source code for the examples is available over on GitHub.