O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Manipulate Strings Quickly with StringBuilder

Problem

You need to perform a repeated string manipulation, and you want to optimize performance.

Solution

Use the System.Text.StringBuilder class to perform your string operations, and convert the final result into a string by calling ToString.

Discussion

Ordinary .NET strings are immutable. Changing just a single character causes the whole string to be thrown away and a new string to be created.

The StringBuilder class represents a buffer of characters that can be directly modified. These direct modifications are faster than repeatedly generating a new String object. However, using the StringBuilder might introduce a small overhead because you need to copy the string into the StringBuilder before performing ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required