csci 3327 visual basic chapter 12: strings, characters and regular expressions utpa – fall 2011

20
CSCI 3327 Visual Basic CSCI 3327 Visual Basic Chapter 12: Strings, Chapter 12: Strings, Characters and Regular Characters and Regular Expressions Expressions UTPA – Fall 2011

Upload: harry-parker

Post on 12-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

CSCI 3327 Visual Basic CSCI 3327 Visual Basic Chapter 12: Strings, Characters Chapter 12: Strings, Characters

and Regular Expressionsand Regular Expressions

UTPA – Fall 2011

Page 2: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

Objectives

• In this chapter, you will– Learn how to create and manipulate String objects– Get familiar with common methods of String class– Learn how to create and manipulate StringBuilder

objects– Know how to use regular expressions in

conjunction with classes Regex and Match– Learn how to search for patterns in text using

regular expressions

2

Page 3: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

Online Chapters

• URL:– www.pearsonhighered.com/deitel/– Chapter 17 - Download: PDF

3

Page 4: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

Strings

• A string is a series of characters treated as a single unit– Characters:

• uppercase/lowercase letters

• digits

• special characters (e.g. +, -, *, /, $ etc.)

4

Page 5: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

Characters

• A character literal is a character that is represented internally as an integer value, called a character code– ASCII code:

• "a" 97• "z" 122• """ 34

– In Visual Basic, letter c following the closing double quote is the syntax for character literal

• "a"c• ","c -- parameter of the Split function

5

Page 6: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

String Constructors

• Dim string0 As String = “hello”• Dim string1 As String = string0• Dim string2 As New String(characterArray)• Dim string3 As New String(characterArray, 6, 3)• Dim string4 As New String("?", 5)

6

Page 7: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

Example 17.1: StringConstructor.vb

• URL:– http://media.pearsoncmg.com/ph/esm/deitel/vb_ht

p_2010/codeexamples.html

• Constructors– Function Chr(34) – output double-quote (")

character– Console.WriteLine("The word ""four"" contains 4

characters")• Output: The word "four" contains 4 characters

7

Page 8: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

String Indexer and Length Property

• Dim string1 As String = "Hello there!"

• Length property– string1.Length

• String indexer – string1(i)– i is index from 0 to string1.Length-1

8

Page 9: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

CopyTo Method of String

• Dim string1 As String = "Hello there!"• Dim characterArray() As Char = New Char (5) {}• CopyTo method

– string1.CopyTo(0, characterArray, 0, 5)• First argument: beginning position of copying characters in the

string1

• Second argument: character array into which characters are copied

• Third argument: starting position to copy to in character array

• Fourth argument: the number of characters to be copied from string1

9

Page 10: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

Example 17.2: StringMethod.vb

• URL:– http://media.pearsoncmg.com/ph/esm/deitel/vb_ht

p_2010/codeexamples.html

10

Page 11: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

Comparing Strings

• string1.Equals(“hello”)• string1 = “hello”• String.Equals(string1, string2)• string1.CompareTo(string2)

– If string1 < string2, -1– If string1 > string2, 1– If string1 = string2, 0

• Note: Nothing is a Null reference, and cannot be compared with an empty string (String.Empty or "")

11

Page 12: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

Example 17.3: StringCompare.vb

• URL:– http://media.pearsoncmg.com/ph/esm/deitel/

vb_htp_2010/codeexamples.html

12

Page 13: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

Example 17.4: StringStartEnd.vb

• URL:– http://media.pearsoncmg.com/ph/esm/deitel/

vb_htp_2010/codeexamples.html

• element.StartWith("st")

• element.EndWith("ed")

13

Page 14: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

Example 17.5: StringIndexMethods

• URL:– http://media.pearsoncmg.com/ph/esm/deitel/

vb_htp_2010/codeexamples.html

• letters.IndexOf("c"c)

• letters.LastIndexOf("c"c)

• letters.IndexOfAny(searchLetters)

• letters.LastIndexOfAny(searchLetters)

14

Page 15: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

Extracting Substrings From Strings

• string1.Substring(arg1)– arg1 – the starting index to copy from string1– If index is outside the bounds of string1, then an

ArgumentOutOfRangeException occurs

• string1.Substring(arg1, arg2)– arg1 – the starting index to copy from string1– arg1 – the length of substring to copy• Example 17.6 (Substring.vb):• http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/

codeexamples.html

15

Page 16: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

Example 17.7: Concatenating Strings

• URL:– http://media.pearsoncmg.com/ph/esm/deitel/

vb_htp_2010/codeexamples.html

• string1 & string2

• String.Concat(string1, string2)

16

Page 17: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

Other Methods of Strings

• string1.Replace("e"c, "E"c)

• string1.ToLower()

• string1.ToUpper()

• string1.Trim()– Remove whitespaces at the beginning and end of a

String

17

Page 18: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

Example 17.8: StringMethods2.vb

• URL:– http://media.pearsoncmg.com/ph/esm/deitel/

vb_htp_2010/codeexamples.html

• Replace

• ToLower

• ToUpper

• Trim

18

Page 19: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

String Class vs. StringBuilder Class

• String’s contents can never change– Operations that seem to concatenate Strings are in

fact creating new Strings

• StringBuilder class is different

19

Page 20: CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011

20