Chapter 13 Exploring Text Files and String Processing 327
processing the textual elements in your programs. In this section, you’ll learn about several
ways to process strings.
The most common task you’ve accomplished so far with strings in this book is concatenating
them by using the concatenation operator (&). For example, the following program
statement concatenates three literal string expressions and assigns the result “Bring on the
circus!” to the string variable Slogan:
Dim Slogan As String
Slogan = "Bring" & " on the " & "circus!"
You can also concatenate and manipulate strings by using methods in the String class of
the .NET Framework library. For example, the String.Concat method allows equivalent string
concatenation by using this syntax:
Dim Slogan As String
Slogan = String.Concat("Bring", " on the ", "circus!")
Visual Basic 2010 features two methods for string concatenation and many other
string-processing tasks: You can use operators and functions from earlier versions of
Visual Basic (Mid, UCase, LCase, and so on), or you can use newer methods from the .NET
Framework (Substring, ToUpper, ToLower, and so on). There’s no real penalty for using either
string-processing technique, although the older methods exist primarily for compatibility
purposes. (By supporting both methods, Microsoft hopes to welcome upgraders and their
existing code base, allowing them to learn new features at their own pace .) In the rest of this
chapter, I’ll focus on the newer string-processing functions from the .NET Framework String
class. However, you can use either string-processing method or a combination of both.
Table 13-2 lists several methods and one property in the String class that appear in
subsequent exercises and their close equivalents in the Visual Basic programming language.
The fourth column in the table provides sample code using the String class.
TABLE 13-2 Elements of the String Class and Visual Basic Equivalents
String
Method or
Property
Visual
Basic
Function Description String Example
ToUpper UCase Changes letters in a string
to uppercase.
Dim Name, NewName As String
Name = "Kim"
NewName = Name.ToUpper
'NewName = "KIM"
ToLower LCase Changes letters in a string
to lowercase.
Dim Name, NewName As String
Name = "Kim"
NewName = Name.ToLower
'NewName = "kim"
Length Len Determines the number
of characters in a string.
Dim River As String
Dim Size As Short
River = "Mississippi"
Size = River.Length
'Size = 11