30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 15 Strings, Characters and Regular Expressions 647<br />

cally <strong>to</strong> those described above except that they search for sequences of characters (or substrings)<br />

that are specified by their String arguments.<br />

Lines 55–79 use methods IndexOfAny and LastIndexOfAny, which take an<br />

array of characters as the first argument. These versions of the methods also perform identically<br />

<strong>to</strong> those described above except that they return the index of the first occurrence of<br />

any of the characters in the character array argument.<br />

Common <strong>Program</strong>ming Error 15.2<br />

In the overloaded methods LastIndexOf and LastIndexOfAny that take three parameters,<br />

the second argument must always be bigger than or equal <strong>to</strong> the third argument. This<br />

might seem counterintuitive, but remember that the search moves from the end of the string<br />

<strong>to</strong>ward the start of the string. 15.2<br />

15.8 Extracting Substrings from Strings<br />

Class String provides two Substring methods, which are used <strong>to</strong> create a new<br />

String object by copying part of an existing String object. Each method returns a new<br />

String object. The application in Fig. 15.7 demonstrates the use of both methods.<br />

1 ' Fig. 15.7: SubString.vb<br />

2 ' Demonstrating the String Substring method.<br />

3<br />

4 Imports System.Windows.Forms<br />

5<br />

6 Module modSubString<br />

7<br />

8 Sub Main()<br />

9 Dim letters As String = "abcdefghijklmabcdefghijklm"<br />

10 Dim output As String<br />

11 Dim quotes As Char = ChrW(34)<br />

12<br />

13 ' invoke SubString method and pass it one parameter<br />

14 output = "Substring from index 20 <strong>to</strong> end is " & _<br />

15 quotes & letters.Substring(20) & quotes & vbCrLf<br />

16<br />

17 ' invoke SubString method and pass it two parameters<br />

18 output &= "Substring from index 0 <strong>to</strong> 6 is " & _<br />

19 quotes & letters.Substring(0, 6) & quotes<br />

20<br />

21 MessageBox.Show(output, _<br />

22 "Demonstrating String method Substring")<br />

23 End Sub ' Main<br />

24<br />

25 End Module ' modSubString<br />

Fig. 15.7 Substrings generated from Strings.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!