Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

Chapter 13 Exploring Text Files and String Processing 329


String
Method or
Property

Visual
Basic
Function Description String Example
StartsWith N/A Determines whether
a string starts with a
specified string.

Dim str1 As String
Dim result As Boolean
str1 = "Hi Felix"
result = str1.StartsWith("Hi")
'result = True
EndsWith N/A Determines whether a string
ends with a specified string.

Dim str1 As String
Dim result As Boolean
str1 = "Hi Felix"
result = str1.EndsWith("Felix")
'result = True
Split Split Splits a string into substrings
based on a specified
separator and puts the
substring in an array.

Dim AllText As String = _
"a*b*c*1*2*3"
Dim strArray() As String
strArray = AllText.Split("*")
'strArray =
' {"a", "b", "c", "1", "2", "3"}

Sorting Text


An extremely useful skill to develop when working with textual elements is the ability to sort
a list of strings. The basic concepts in sorting are simple. You draw up a list of items to sort
and then compare the items one by one until the list is sorted in ascending or descending
alphabetical order.

In Visual Basic, you compare one item with another by using the same relational operators
that you use to compare numeric values. The tricky part (which sometimes provokes
long-winded discussions among computer scientists) is the specific sorting algorithm that
you use to compare elements in a list. We won’t get into the advantages and disadvantages
of different sorting algorithms in this chapter. (The bone of contention is usually speed,
which makes a difference only when several thousand items are sorted .) Instead, we’ll explore
how the basic string comparisons are made in a sort. Along the way, you’ll learn the skills
necessary to sort your own text boxes, list boxes, files, and databases.

Before Visual Basic can compare one character with another in a sort, it must convert each
character into a number by using a translation table called the ASCII character set (also
called the ANSI character set). (The acronym ASCII stands for American Standard Code for
Information Interchange .) Each of the basic symbols that you can display on your computer
has a different ASCII code. These codes include the basic set of “typewriter” characters
(codes 32 through 127) and special “control” characters, such as tab, line feed, and carriage
return (codes 0 through 31). For example, the lowercase letter a corresponds to the ASCII
code 97, and the uppercase letter A corresponds to the ASCII code 65. As a result, Visual Basic
treats these two characters quite differently when sorting or performing other comparisons.
Free download pdf