O'Reilly logo

Visual Basic 2005 Cookbook by Tim Patrick, John Clark Craig

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

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( )) ' ----- ...

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