You can use .ToString() to get the String from the StringBuilder.

Answer from Patrick Desjardins on Stack Overflow
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.text.stringbuilder.tostring
StringBuilder.ToString Method (System.Text) | Microsoft Learn
Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString()) You must call the ToString method to convert the StringBuilder object to a String object before you can pass the string represented by the StringBuilder object to a method that has ...
Discussions

stringbuilder to string[]
I am using the kernel function to read dot-ini sections and section values. The function ReadPrivateProfileSection returns a 'stringbuilder' value which is a series of strings representing the key=value items of the section as strings (null terminated) and terminated with a final null. I want... More on tek-tips.com
🌐 tek-tips.com
3
0
October 12, 2013
strings - StringBuilder in C - Code Review Stack Exchange
Lots of languages have something that lets you build up a dynamically-sized string with minimal overhead. C doesn't, and I found myself using code that did that manually in a couple of places, so I More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
February 14, 2017
String builders in C
They aren’t standard (yet), but widely available are fmemopen and open_memstream. And asprintf can handle simpler cases. It’s worth familiarizing yourself with all the dynamic memory extensions functions and checking to see if they’re already available on your platform. More on reddit.com
🌐 r/C_Programming
9
19
June 3, 2023
How to convert a StringBuilder to a String
I'm moderately confused. Doesn't split turn it into an array? More on reddit.com
🌐 r/shittyprogramming
28
108
August 2, 2016
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.text.stringbuilder
StringBuilder Class (System.Text) | Microsoft Learn
If you encounter performance issues ... any of the following workarounds: Convert the StringBuilder instance to a String by calling the ToString method, then access the characters in the string....
🌐
Dot Net Perls
dotnetperls.com › stringbuilder-tostring
C# - StringBuilder ToString: Get String - Dot Net Perls
ToString on StringBuilder returns a string. It internally converts the character buffer of the StringBuilder into a string object.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › standard › base-types › stringbuilder
Using the StringBuilder Class in .NET - .NET | Microsoft Learn
If you set the Length property to a value that is greater than the Capacity property, the Capacity property is automatically changed to the same value as the Length property. Setting the Length property to a value that is less than the length of the string within the current StringBuilder shortens the string.
🌐
Webdevtutor
webdevtutor.net › blog › c-sharp-stringbuilder-to-string
Converting StringBuilder to String in C#
The most straightforward way to convert a StringBuilder to a string is by using the ToString method provided by the StringBuilder class.
🌐
Peter's blog
petercodes.hashnode.dev › difference-between-string-and-stringbuilder-in-c
difference between string and string builder in C# - Peter's blog
January 15, 2023 - To set the capacity, you need to pass a length value in the StringBuilder method: ... In the above code, the StringBuilder class will accommodate only 20 characters. After the capacity is reached, a new space for the StringBuilder will be allocated in the heap. Setting StringBuilder capacity can increase performance dramatically but will use more memory if capacity is a lot larger than final string length. In C#, we can convert a StringBuilder object to a string by using the methods below:
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › c# › stringbuilder-tostring-method-in-c-sharp
StringBuilder.ToString Method in C# - GeeksforGeeks
July 11, 2025 - Subsequent changes to this sequence contained by Object do not affect the contents of the String. Syntax: public override string ToString (); Return Value: This method returns the String representing the data contained by StringBuilder Object.
🌐
Blogger
coldfusion-example.blogspot.com › 2025 › 01 › convert-stringbuilder-to-string-in-c.html
Convert StringBuilder to String in C# with Ease
The ToString method retrieves the current content of the StringBuilder instance as a string. While the ToString method is straightforward, consider these best practices for optimal performance and maintainability: Avoid Unnecessary Conversions: Convert StringBuilder to string only when needed.
🌐
Tek-Tips
tek-tips.com › home › forums › software › programmers › languages › c# (c sharp): microsoft
stringbuilder to string[] | Tek-Tips
October 12, 2013 - If it is truly a StringBuilder object that is returned, a simple .ToString() should suffice: string myvalue = stringbuilder.ToString(); Cheers, MakeItSo “Knowledge is power. Information is liberating.
🌐
Codefinity
codefinity.com › blog › What-is-the-StringBuilder-in-C-sharp
What is the StringBuilder in C#
A: Yes, you can convert a StringBuilder object back to a regular string using the ToString() method.
Top answer
1 of 3
7

Bug: Initial allocation not cleared to zero

Your initial allocation of sb->mem uses malloc instead of calloc, so its contents are uninitialized. If you then append a few characters and call sb_as_string(), you will get back a string that is not properly terminated. You should use calloc instead.

Minor bug

If your call to realloc fails, your buffer will be incorrect because it will no longer be null terminated (you just appended a character to the last spot). You should either rewrite a '\0' to the end of the buffer if realloc fails, or do the realloc before you append the character.

Argument check

When creating a string buffer, you should handle the case where init_cap is passed in as 0. You can set it to some default value in that case. Right now, an initial capacity of 0 will cause a crash down the line because your append function will append to a zero length buffer without ever reallocating.

Usability

I would much prefer an append function that took a string argument instead of a character argument. I'm not sure that I would ever need to append one character at a time.

Also, it might be nice to have a to_string type function that returns the string but also frees the stringbuilder. The way you currently have it, you can retrieve the string, but if you subsequently free the stringbuilder it will also free the string you just retrieved. That makes it difficult to use the string because its lifetime is tied to the lifetime of the stringbuilder.

2 of 3
3

To be more comprehensive, the line in sb_append():

memset(new_mem + to->cap, 0, to->cap);

should become:

memset(new_mem + to->cap, 0, (to->cap) * (LOAD_FACTOR - 1));

That matters when LOAD_FACTOR is set to something greater than 2.

thanks for sharing this code.

🌐
IronPDF
ironpdf.com › ironpdf blog › .net help › string builder c#
String Builder C# (How It Works For Developers)
April 22, 2026 - First, a StringBuilder object named contentBuilder constructs the PDF content. Text is appended using AppendLine. Then, the content in StringBuilder is converted to a string named pdfContent. The ChromePdfRenderer from IronPDF is instantiated as renderer, and the RenderHtmlAsPdf method generates a PDF document from the content.
🌐
Andrew Lock
andrewlock.net › a-deep-dive-on-stringbuilder-part-3-converting-chunks-to-a-string-with-tostring
Converting chunks to a string with ToString(): A deep dive on StringBuilder - Part 3
July 27, 2021 - Once you've added all your partial strings to StringBuilder, you call ToString() to get the final string out. In simple cases, that may simply involve converting a char[] buffer into a string. But remember that under-the-hood, StringBuilder is implemented as a singly linked list, so we may have multiple chunks that need to be combined.
🌐
Reddit
reddit.com › r/c_programming › string builders in c
r/C_Programming on Reddit: String builders in C
June 3, 2023 -

I've written a bunch of code that prints values of various different types which works great but rather than printing to stdout I'd like to capture the result in a big string. In other languages this would be achieved by appending to a string builder or appending strings to an array and then concatenating them all. Is there an idiomatic way to do this in vanilla C? If not, what library would you recommend (M1/M2 Mac OS and Raspberry Pi)?

🌐
Reddit
reddit.com › r/shittyprogramming › how to convert a stringbuilder to a string
r/shittyprogramming on Reddit: How to convert a StringBuilder to a String
August 2, 2016 - ... Not trolling, I'm fully aware of how indexing works. The issue here is that's not how substring works: the end index is not included. To get a "substring" of the entire string, you specify start=0 and end=length.
🌐
Post.Byes
post.bytes.com › home › forum › topic › c sharp › how to convert from string to stringbuilder?
How to convert from string to stringbuilder? - Post.Byes - Bytes
When having a string that is defined as a stringbuilder it can be converted to a string by this: StringbuilderText.ToString(); But what about the otherway round? Thanks Ole
🌐
PC Review
pcreview.co.uk › newsgroups › microsoft dotnet › microsoft c# .net
How to convert from string to stringbuilder? | PC Review
November 25, 2004 - string s = "Hello"; StringBuilder ... that is defined as a stringbuilder it can be converted to a string by this: StringbuilderText.ToString(); But what about the otherway round?...