Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
136
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

550 Chapter 12 HANDLING STRINGS, CHARACTERS, AND DATES

VB.NET at Work: The CountWords Project

As you have noticed, the StringBuilder doesn’t provide as many methods as the String class. The StringBuilder class should be used to build long strings and manipulate them dynamically. If you want to locate words or other patterns in the text, align strings in fixed-length fields, and other similar operations, use the String class. You can also combine both classes in your application, so that you can speed both string-manipulation and -handling operations. To extract the text from a StringBuilder, use its ToString method. To assign a string to the StringBuilder variable, use its Append method:

Dim strB As New StringBuilder Dim str1, str2 As String str1 = “some text” strB.Append(str1)

{ statements to process the strB variable } str2 = strB.ToString

Any of the String class’s methods, however, can be used with StringBuilder variables. The ToString method of the StringBuilder class returns a string, which can be processed with the methods of the String class. For instance, the StringBuilder class lacks the IndexOf and LastIndexOf methods. To locate an instance of a word in a StringBuilder variable, use the following statement:

pos = SB.ToString.IndexOf(“visual”)

where SB is a properly declared an initialized StringBuilder variable and pos is the index of the first instance of the word “visual” in the StringBuilder’s text.

The CountWords application, shown in Figure 12.2, counts all instances of a user-supplied word in a StringBuilder variable. You can do the same with the Sting class, but if you want to further process the text, you’ll have to use the StringBuilder class anyway. The program prompts the user for a string and attempts to locate it in the text with the following statement:

startIndex = SB.ToString.ToUpper.IndexOf(searchWord.ToUpper)

Figure 12.2

The CountWords project counts the instances of a usersupplied word in a text.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

HANDLING STRINGS AND CHARACTERS 551

Then, it sets up a loop that locates one instance of the user-supplied word at a time. The following statement searches for the word in text, starting at the location startIndex + searchWord.Length + 1. This expression is the location of the first character following the most recently located instance of the word in the large string. At each iteration of the loop, the IndexOf method starts searching for the word in the text following the previous instance of the word. Here’s the statement that locates the next instance of the word in the text:

startIndex = SB.ToString.ToUpper.IndexOf(searchWord.ToUpper, _

startIndex + searchWord.length + 1)

This statement appears in a loop that’s repeated for as long as the startIndex variable is positive. When all instances of the word in the text have been located, the IndexOf method returns the value –1 and the loop terminates. The complete code of the Count Words button is shown in Listing 12.7.

Listing 12.7: The CountWords Project’s Code

Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click Dim SB As New System.[Text].StringBuilder()

Dim searchWord As String

searchWord = InputBox(“Please enter the word to search for”, _ “StringBuilder Search Example”, “BASIC”)

Dim startIndex As Integer SB.Append(Textbox1.Text)

startIndex = SB.ToString.ToUpper.IndexOf(searchWord.ToUpper) Dim T1, T2 As Date

Dim SP As New TimeSpan() T1 = Now()

Dim count As Integer If startIndex = 0 Then

MsgBox(“The string you’re searching for wasn’t found”) End If

While startIndex > 0 And startIndex + searchWord.Length < SB.Length count = count + 1

startIndex = SB.ToString.ToUpper.IndexOf(searchWord.ToUpper, _ startIndex + searchWord.Length + 1)

End While

T2 = Now

SP = T2.Subtract(T1) Dim msg As String

msg = “Located “ & count.ToString & “ instances of the word in “ & _ SP.Milliseconds.ToString & “ milliseconds”

MsgBox(msg) End Sub

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com