Chapter 14. Working with Strings

Previous lessons provided a sneak peek at some of the things that a Visual Basic program can do with strings. Lesson 11 explained how you can use the & operator to concatenate two strings. Several lessons show how to use the ToString method to convert numeric values into strings that you can then display to the user.

In this lesson, you learn a lot more about strings. You learn about String class methods that let you search strings, replace parts of strings, and extract pieces of strings. You also learn new ways to format numeric and other kinds of data to produce strings.


The String class provides a lot of useful methods for manipulating strings. For example, the EndsWith method returns True if a string ends with a particular substring. The following code determines whether a string ends with the substring dog:

Dim str As String = "The quick brown fox jumps over the lazy dog"
MessageBox.Show("Ends with dog: " & str.EndsWith("dog"))

Table 14-1 summarizes the String class's most useful methods.

Table 14.1. TABLE 14-1




Returns True if the string contains a target string.


Returns True if the string ends with a target string.


Returns the index of a target character or string within the string.


Returns the index of the first occurrence of any of a set of characters in the string.


Inserts text in the middle of the string.


Returns the index of the last occurrence of a target character or string ...

Get Stephens' Visual Basic® Programming 24-Hour Trainer now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.