Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011."— Presentation transcript:

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

2 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

3 Online Chapters URL: –www.pearsonhighered.com/deitel/ –Chapter 17 - Download: PDF 3

4 Strings A string is a series of characters treated as a single unit –Characters: uppercase/lowercase letters digits special characters (e.g. +, -, *, /, $ etc.) 4

5 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

6 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

7 Example 17.1: StringConstructor.vb URL: –http://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.htmlhttp://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

8 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

9 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

10 Example 17.2: StringMethod.vb URL: –http://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.html 10

11 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

12 Example 17.3: StringCompare.vb URL: –http://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.html 12

13 Example 17.4: StringStartEnd.vb URL: –http://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.html element.StartWith("st") element.EndWith("ed") 13

14 Example 17.5: StringIndexMethods URL: –http://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.html letters.IndexOf("c"c) letters.LastIndexOf("c"c) letters.IndexOfAny(searchLetters) letters.LastIndexOfAny(searchLetters) 14

15 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/codee xamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codee xamples.html 15

16 Example 17.7: Concatenating Strings URL: –http://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.html string1 & string2 String.Concat(string1, string2) 16

17 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

18 Example 17.8: StringMethods2.vb URL: –http://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.html Replace ToLower ToUpper Trim 18

19 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

20 20


Download ppt "CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011."

Similar presentations


Ads by Google