Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW