• No results found

Frequently Used String Class Methods

Although there are many operations you can perform on strings, a small set of operations dominate. Three of the top operations are 1. finding a substring in a string, 2. determining the length of a string, and 3. determining the position of a character in a string.

The following short program demonstrates how to perform these opera- tions. A String object is instantiated to the string “Hello world”. We then break the string into its two constituent pieces: the first word and the second word. Here’s the code:

Module Module1 Sub Main()

Dim string1 As New String("Hello world") Dim len As Integer = string1.Length() Dim pos As Integer

Working with the String Class 153

Dim firstWord, secondWord As String firstWord = string1.Substring(0, pos) secondWord = string1.Substring(pos + 1, _

len - (pos + 1)) Console.WriteLine("First word: " & firstWord) Console.WriteLine("Second word: " & secondWord) Console.Read()

End Sub End Module

The first thing we do is use the Length method to determine the length of the object string1. The length is simply the total number of all the characters in the string. We’ll explain shortly why we need to know the length of the string.

To break up a two-word phrase into separate words we need to know what separates the words. In a well-formed phrase, a space separates words, so we want to find the space between the two words in this phrase. We can do this with the IndexOf method. This method takes a character and returns the character’s position in the string. Strings in VB.NET are zero-based, so the first character in the string is at position 0, the second character is at position 1, and so on. If the character can’t be found in the string, a −1 is returned.

The IndexOf method finds the position of the space separating the two words and is used in the next method, Substring, to actually pull the first word out of the string. The Substring method takes two arguments: a starting position and the number of characters to pull. Look at the following example:

Dim s As New String("Now is the time") Dim sub As String = s.Substring(0,3)

The value of sub is “Now”. The Substring method will pull as many characters out of a string as you ask it to, but if you try to go beyond the end of the string, an exception is thrown.

The first word is pulled out of the string by starting at position 0 and pulling outposnumber of characters. This may seem odd, since pos holds the position of the space, but since strings are zero-based, this is the correct number.

The next step is to pull out the second word. Since we know where the space is, we know that the second word starts at pos +1 (again, we’re assuming

we’re working with a well-formed phrase where each word is separated by exactly one space). The harder part is deciding exactly how many characters to pull out, knowing that an exception will be thrown if we try to go beyond the end of the string. There is a formula of sorts we can use for this calculation. First, we add 1 to the position where the space was found and then subtract that value from the length of the string. That will tell the method exactly how many characters to extract.

Although this short program is interesting, it’s not very useful. What we really need is a program that will pull the words out of a well-formed phrase of any length. There are several different algorithms we can use to do this.

The algorithm we’ll use here contains the following steps:

1. Find the position of the first space in the string.

2. Extract the word.

3. Build a new string starting at the position past the space and continuing until the end of the string.

4. Look for another space in the new string.

5. If there isn’t another space, extract the word from that position to the end of the string.

6. Otherwise, loop back to step 2.

Here is the code we built from this algorithm (with each word extracted from the string being stored in a collection named words):

Module Module1 Sub Main()

Dim string1 As New String("now is the time") Dim pos As Integer

Dim word As String

Dim words As New Collection pos = string1.IndexOf(" ") While (pos > 0)

word = string1.Substring(0, pos) words.Add(word)

string1 = string1.Substring(pos + 1, string1. _ Length() - (pos + 1)) pos = string1.IndexOf(" ")

Working with the String Class 155

If (pos = -1) Then

word = string1.Substring(0, string1.Length) words.Add(word) End If End While Console.Read() End Sub End Module

Of course, if we were going to actually use this algorithm in a program we’d make it a function and have it return a collection, like this:

Module Module1 Sub Main()

Dim string1 As New String("now is the time for " & _ "all good people")

Dim words As New Collection words = SplitWords(string1) Dim word As Object

For Each word In words Console.WriteLine(word) Next

Console.Read() End Sub

Function SplitWords(ByVal st As String) As Collection Dim word As String

Dim ws As New Collection Dim pos As Integer

pos = st.IndexOf(" ") While (pos > 0)

word = st.Substring(0, pos) ws.Add(word)

st = st.Substring(pos + 1, st.Length - (pos + 1)) pos = st.IndexOf(" ")

If (pos = -1) Then

word = st.Substring(0, st.Length) ws.Add(word)

End While Return ws End Function End Module

However, the String class already has a method for splitting a string into parts (the Split method) as well as a method that can take a data collection and combine its parts into a string (the Join method). We look at those methods in the next section.