1 / 22

UTPA – Fall 2011

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

rivenbark
Télécharger la présentation

UTPA – Fall 2011

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

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

  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

  5. 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

  6. Example 17.9: StringBuilderConstructor.vb • URL: • http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html

  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

  8. 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

  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_htp_2010/codeexamples.html

  10. 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}

  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

  12. Example 17.13: StringBuilderInsertRemove.vb • URL: • http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html

  13. 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)

  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)

  15. Example 17.15: SharedCharMethodsForm.vb • URL: • http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html

  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)

  17. 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”)

  18. Character Classes • \d • Any digit • \w • Any word character • \s • Any whitespace • \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_2010/codeexamples.html • \w+ • \w+? • [a-f] • [^a-f] • [a-zA-Z]+ • .*

  20. Other Quantifiers in Regular Expressions

  21. 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

More Related