TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# StringBuilder ToString: Get String From StringBuilder

Get a string from a StringBuilder instance with the ToString method.
StringBuilder ToString. ToString on StringBuilder returns a string. It internally converts the character buffer of the StringBuilder into a string object. It does this without allocating memory or copying a lot of data in most cases.StringBuilder
First, this program demonstrates the ToString method usage on the StringBuilder type. This method is used to convert a StringBuilder's character buffer into an actual string reference.

And: Because many .NET programs use a string parameter type, converting the StringBuilder's buffer to a string is useful.

Main: This method creates a StringBuilder and appends 13 strings to its buffer using the Append calls.

Finally: We call ToString. This method internally uses logic and often does not copy the string data an additional time.

Instead: ToString sets a null character in the buffer data and transforms the buffer into a string without any allocation occurring.

C# program that uses ToString method on StringBuilder using System; using System.Text; class Program { static void Main() { // // Begin by declaring a StringBuilder and adding three strings. // StringBuilder builder = new StringBuilder(); builder.Append("one "); builder.Append("two "); builder.Append("three "); // // Add many strings to the StringBuilder in a loop. // for (int i = 0; i < 10; i++) { builder.Append("x "); } // // Get a string from the StringBuilder. // ... Often no additional copying is required at this stage. // string result = builder.ToString(); Console.WriteLine(result); } } Output one two three x x x x x x x x x x
Copying. Let's explore whether the ToString instance override method on the StringBuilder class actually makes another copy of the string data. The ToString method on StringBuilder contains some logic that avoids an additional copy in many cases.

Note: An additional copy of the string characters is made when the StringBuilder is being used on multiple threads at the same time.

An additional copy is also made if the memory allocated to the StringBuilder is much larger than required. This improves efficiency. It means that the copy will occupy less memory in the long run.

However: In all other cases, the ToString method simply modifies its internal buffer and transforms it into a string.

Summary. We examined the StringBuilder type and its ToString method, which is useful for converting the internal character buffer to a usable string type in the C# language. ToString internally prevents an extra string copy from occurring.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf