Chapter 5. Strings

Introduction

Every Visual Basic developer quickly learns how to manipulate strings, but it’s often easy to overlook some of the more powerful techniques available, especially with all the new features in Visual Basic 2005. A good example is the powerful StringBuilder object, which provides an order-of-magnitude improvement for concatenating strings. Visual Basic 6 developers, in particular, will discover lots of exciting new string-processing features. For example, Visual Basic 2005’s Substring() method provides similar functionality not only to the Mid() function, but also to the Left() and Right() string functions. The regular expression library included with .NET also provides new and powerful ways to analyze and process string data.

5.1. Using a StringBuilder

Problem

You need to process many pieces of string data with more efficiency than is allowed using standard .NET Framework immutable strings.

Solution

The StringBuilder object provides extremely fast and efficient in-place processing of string and character data. The following code demonstrates several of its powerful methods and some of the techniques you can use to speed up your string processing:

	Dim workText As New System.Text.StringBuilder
	
	' ----- Build a basic text block.
	workText.Append("The important")
	workText.Append(vbNewLine)
	workText.Append("thing is not")
	workText.AppendLine()
	workText.AppendLine("to stop questioning.")

	workText. Append("--Albert Einstein") MsgBox(workText.ToString( )) ' ----- ...

Get Visual Basic 2005 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.