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 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 4

5 StringBuilder Class Imports System.Text Constructor –Dim buffer1 As New StringBuilder() –Dim buffer2 As New StringBuilder(10) –Dim buffer3 As New StringBuilder(“hello”) Initial capacity is the smallest power of two greater than or equal to the number of characters in the argument with a minimum of 16 5

6 Example 17.9: StringBuilderConstructor.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 6

7 Length and Capacity Properties Length property –The number of characters in a StringBuilder object Capacity property –The number of characters that a StringBuilder object can store without allocating more memory 7

8 Example 17.10: StringBuilderFeatures.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 Function EnsureCapacity(75) Truncate StringBuilder length –buffer.Length = 10 8

9 Append Method Each method has versions for primitive type and for character arrays, Strings and Objects –buffer.Append(“hello”) –buffer.Append(objectValue) –buffer.Append(characterArray) –buffer.Append(characterArray, 0, 3) Example 17.11: StringBuilderAppend.vb –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 9

10 Example 17.12: AppendFormat Method URL: –http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/c odeexamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/c odeexamples.html string1 = “this {0} costs: {1:C}.” buffer.AppendFormat(string1, objectArray) {X[, Y][:FormatString]} –The number of the argument to be formatted (starting from zero) –Y is an optional argument –The String will be padded with spaces {0:D3} – three-digit decimal {0, 4}, {0, -4} 10

11 Insert and Remove Methods Insert –Primitive types –Character arrays, Strings and Objects Remove(arg1, arg2) –arg1 – the index at which to begin deletion –arg2 – the number of characters to delete 11

12 Example 17.13: StringBuilderInsertRemove.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.14: Replace Method 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 builder.Replace(string1, string2) builder.Replace(“g”c, “G”c, index, count) 13

14 Char Methods Methods of structure Char –Char.IsDigit(character) –Char.IsLetter(character) –Char.IsLower(character) –Char.IsUpper(character) –Char.IsPunctuation(character) –Char.IsWhiteSpace(character) 14

15 Example 17.15: SharedCharMethodsForm.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 15

16 Regular Expression Formatted strings –E.g. zip code – five digits Imports System.Text.RegularExpressions Regex object –Dim expression As New Regex(“e”) –expression.Match(testString) –expression.Matches(testString) 16

17 Example 17.16: BasicRegex.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 For Each myMatch In expression.Matches(testString) Quantifier Dim expression As New Regex(“regexp?”) –Alternation Dim expression As New Regex(“(c|h)at”) 17

18 Character Classes \d –Any digit \w –Any word character \s –Any whitespace 18 \D –Any nondigit \W –Any nonword character \S –Any nonwhitespace

19 Example 17.18: CharacterClasses.vb URL: –http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_20 10/codeexamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_htp_20 10/codeexamples.html \w+ \w+? [a-f] [^a-f] [a-zA-Z]+.* 19

20 Other Quantifiers in Regular Expressions 20

21 Example 17.20: Validate.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 [\d-[4]] –Any digits other than 4 21

22 22


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

Similar presentations


Ads by Google