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.

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

character set (see Appendix E, ASCII character set). To learn the integer equivalents of<br />

many common Unicode characters, see Appendix F, Unicode.<br />

A string is a series of characters treated as a single unit. These characters can be uppercase<br />

letters, lowercase letters, digits and various special characters, such as +, -, *, /, $<br />

and others. A string is an object of class String in the System namespace. We write<br />

string literals, or string constants (often called literal String objects), as sequences of<br />

characters in double quotation marks, as follows:<br />

"John Q. Doe"<br />

"9999 Main Street"<br />

"Waltham, Massachusetts"<br />

"(201) 555-1212"<br />

A declaration can assign a String literal <strong>to</strong> a String reference. The declaration<br />

Dim color As String = "blue"<br />

initializes String reference color <strong>to</strong> refer <strong>to</strong> the String literal object "blue".<br />

Performance Tip 15.1<br />

If there are multiple occurrences of the same String literal object in an application, a single<br />

copy of the String literal object will be referenced from each location in the program<br />

that uses that String literal. It is possible <strong>to</strong> share the object in this manner, because<br />

String literal objects are implicitly constant. Such sharing conserves memory. 15.1<br />

15.3 String Construc<strong>to</strong>rs<br />

Class String provides three construc<strong>to</strong>rs for initializing String objects in various<br />

ways. Figure 15.1 demonstrates the use of three of the construc<strong>to</strong>rs.<br />

1 ' Fig. 15.1: StringConstruc<strong>to</strong>r.vb<br />

2 ' Demonstrating String class construc<strong>to</strong>rs.<br />

3<br />

4 Imports System.Windows.Forms<br />

5<br />

6 Module modStringConstruc<strong>to</strong>r<br />

7<br />

8 Sub Main()<br />

9 Dim characterArray As Char()<br />

10 Dim output As String<br />

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

12 Dim originalString, string1, string2, string3, _<br />

13 string4 As String<br />

14<br />

15 characterArray = New Char() {"b"c, "i"c, "r"c, _<br />

16 "t"c, "h"c, " "c, "d"c, "a"c, "y"c}<br />

17<br />

18 ' string initialization<br />

19 originalString = "Welcome <strong>to</strong> VB.<strong>NET</strong> <strong>Program</strong>ming!"<br />

20 string1 = originalString<br />

21 string2 = New String(characterArray)<br />

Fig. 15.1 String construc<strong>to</strong>rs (part 1 of 2).

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

Saved successfully!

Ooh no, something went wrong!