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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

31<br />

32 ' copy characters from string1 in<strong>to</strong> characterArray<br />

33 string1.CopyTo(0, characterArray, 0, 5)<br />

34 output &= vbCrLf & "The character array is: "<br />

35<br />

36 For i = 0 To characterArray.GetUpperBound(0)<br />

37 output &= characterArray(i)<br />

38 Next<br />

39<br />

40 MessageBox.Show(output, "Demonstrating String" & _<br />

41 " properties Length and Chars", _<br />

42 MessageBoxBut<strong>to</strong>ns.OK, MessageBoxIcon.Information)<br />

43 End Sub ' Main<br />

44<br />

45 End Module ' modMiscellaneous<br />

Fig. 15.2 String Length and Chars properties, and CopyTo method (part 2<br />

of 2).<br />

In this example, we create an application that determines the length of a String,<br />

reverses the order of the characters in the String and copies a series of characters from<br />

the String in<strong>to</strong> a character array.<br />

Line 22 uses String property Length <strong>to</strong> determine the number of characters in<br />

String string1. Like arrays, Strings always know their own size.<br />

Lines 28–30 append <strong>to</strong> output the characters of the String string1 in reverse<br />

order. The String property Chars returns the character located in a specific index in the<br />

String. Property Chars takes an Integer argument specifying the index and returns<br />

the character at that index. As in arrays, the first element of a String is at index 0.<br />

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

Attempting <strong>to</strong> access a character that is outside the bounds of a String (i.e., an index less<br />

than 0 or an index greater than or equal <strong>to</strong> the String’s length) results in an Index-<br />

OutOfRangeException. 15.1<br />

Line 33 uses String method CopyTo <strong>to</strong> copy the characters of a String<br />

(string1) in<strong>to</strong> a character array (characterArray). The first argument given <strong>to</strong><br />

method CopyTo is the index from which the method begins copying characters in the<br />

String. The second argument is the character array in<strong>to</strong> which the characters are copied.<br />

The third argument is the index specifying the location at which the method places the<br />

copied characters in the character array. The last argument is the number of characters that<br />

the method will copy from the String. Lines 36–38 append the Char array contents <strong>to</strong><br />

String output one character at a time.

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

Saved successfully!

Ooh no, something went wrong!