220 likes | 238 Views
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
E N D
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
Online Chapters • URL: • www.pearsonhighered.com/deitel/ • Chapter 17 - Download: PDF
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
StringBuilder Class • Imports System.Text • Constructor • Dim buffer1 AsNew StringBuilder() • Dim buffer2 AsNew StringBuilder(10) • Dim buffer3 AsNew 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
Example 17.9: StringBuilderConstructor.vb • URL: • http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html
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
Example 17.10: StringBuilderFeatures.vb • URL: • http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html • Function EnsureCapacity(75) • Truncate StringBuilder length • buffer.Length = 10
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_htp_2010/codeexamples.html
Example 17.12: AppendFormat Method • URL: • http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.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}
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
Example 17.13: StringBuilderInsertRemove.vb • URL: • http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html
Example 17.14: Replace Method • URL: • http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html • builder.Replace(string1, string2) • builder.Replace(“g”c, “G”c, index, count)
Char Methods • Methods of structure Char • Char.IsDigit(character) • Char.IsLetter(character) • Char.IsLower(character) • Char.IsUpper(character) • Char.IsPunctuation(character) • Char.IsWhiteSpace(character)
Example 17.15: SharedCharMethodsForm.vb • URL: • http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html
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)
Example 17.16: BasicRegex.vb • URL: • http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html • ForEach myMatch In expression.Matches(testString) • Quantifier • Dim expression As New Regex(“regexp?”) • Alternation • Dim expression As New Regex(“(c|h)at”)
Character Classes • \d • Any digit • \w • Any word character • \s • Any whitespace • \D • Any nondigit • \W • Any nonword character • \S • Any nonwhitespace
Example 17.18: CharacterClasses.vb • URL: • http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html • \w+ • \w+? • [a-f] • [^a-f] • [a-zA-Z]+ • .*
Example 17.20: Validate.vb • URL: • http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html • [\d-[4]] • Any digits other than 4