asp.net programming with c# and sql server first edition chapter 5 manipulating strings with c#
Post on 31-Dec-2015
229 Views
Preview:
TRANSCRIPT
Objectives
In this chapter, you will:
• Manipulate strings
• Parse strings
• Work with regular expressions
ASP.NET Programming with C# and SQL Server, First Edition 2
Introduction to Manipulating Strings
• Form data is submitted as strings, so you must learn to deal with strings
• Regular expressions are used for matching and manipulating strings according to specified rules
ASP.NET Programming with C# and SQL Server, First Edition 3
Manipulating Strings
• String: text contained within double quotation marks– Can be used as literal values or can be assigned to
variables
• Parsing: the act of extracting characters or substrings from a larger string– A Web page document is one large text string that
includes formatting and other information that must be extracted
– Browser parses the formatting information from a Web page before displaying the page
ASP.NET Programming with C# and SQL Server, First Edition 4
Manipulating Strings (cont’d.)
• String class: contains methods and properties for manipulating strings
• System.Net.Mail namespace: contains classes for sending e-mail to an SMTP server
• Simple Mail Transfer Protocol (SMTP): a protocol used by most e-mail systems to send messages over the Internet
• Namespace: an abstract container that manages identifiers in a C# program– Used to uniquely identify two elements with the
same nameASP.NET Programming with C# and SQL Server, First Edition 5
Manipulating Strings (cont’d.)
• @import processing directive: used to make a namespace available for use in an ASP.NET Web page
• Example: <%@ Import Namespace=“System.Net.Mail” %>
• using keyword: used as a directive to import classes defined in other namespaces into the current class file
ASP.NET Programming with C# and SQL Server, First Edition 6
Manipulating Strings (cont’d.)
• MailMessage class: used to create an e-mail object containing sender, recipient, subject, and bodyMailMessage emailObject = new MailMessage();
• MailAddress class: used to identify the e-mail address and display name of the sender or recipientMailAddress from = new MailAddress (“support@myBiz.com”, “myBiz Support Team”);
– Use Add() method to assign MailAddress objects to the To, CC, and Bcc properties, but not the From property
ASP.NET Programming with C# and SQL Server, First Edition 7
Manipulating Strings (cont’d.)
• ASP.NET Web Site Administration Tool: used to administer and configure a Web site– Configure SMTP settings to send e-mail
• Must instantiate an SmtpClient class object– Set UseDefaultCredentials to true– Use Send() method, passing the MailMessage
object
ASP.NET Programming with C# and SQL Server, First Edition 8
Counting Characters in a String
• Length property: String class property that returns the number of characters in a string
• IsNullOrEmpty() method: returns true if a string variable contains an empty string, false if the string has 1 or more characters
ASP.NET Programming with C# and SQL Server, First Edition 9
Using Special Characters
• Must use an escape sequence to include basic types of special characters within a literal string
• Examples: – \” includes double quotation marks in the string
• For other types of special characters, use Unicode or XHTML character entities
• Examples:– © is represented by ©– “Copyright” is represented by ©
• Can use escape sequence in form \xhhhh, where hhhh is the hexadecimal value for the Unicode value
ASP.NET Programming with C# and SQL Server, First Edition 10
Changing Case
• ToLower() method: converts a text string to lowercase
• ToUpper() method: converts a text string to uppercase
• Syntax: append the method to the string variable name
• Example: String myName;
Response.Write(“<p>” + myName.toUpper() + “</p>”);
• Original value of the variable is not changed
ASP.NET Programming with C# and SQL Server, First Edition 11
Trimming Strings
• Use trimming methods of the String class to remove spaces or characters from a text string
ASP.NET Programming with C# and SQL Server, First Edition 12
Table 5-1 Trimming methods of the String class
Trimming Strings (cont’d.)
• If no argument is included, the trimming methods will remove spaces
• Can specify which characters to remove
• Can pass a character array argument to the trimming methods to remove any character in the array from the text string
ASP.NET Programming with C# and SQL Server, First Edition 13
Padding Strings
• Use padding methods of the String class to add characters to the beginning or end of a string– Length argument: represents total length of string
after padding, not the number of characters to add
ASP.NET Programming with C# and SQL Server, First Edition 14
Table 5-2 Padding methods of the String class
Parsing Strings
• C# provides several methods for parsing strings
• You can find, extract, and replace individual characters or substrings
ASP.NET Programming with C# and SQL Server, First Edition 15
Finding and Extracting Characters and Substrings
• It is common to extract characters and substrings from strings– Example: extract the name portion of an e-mail
address
• C# has several methods for finding and extracting characters
• Contains() method: returns true if the specified text was found in the string
• StartsWith() method: determines whether the specified text appears at the beginning of the string
ASP.NET Programming with C# and SQL Server, First Edition 16
Finding and Extracting Characters and Substrings (cont’d.)
• EndsWith() method: determines whether the specified text appears at the end of the string
• Some string methods return a numeric position– Character positions in a string start at position 0
• IndexOf() method: returns the position of the first instance of the specified character(s) in the string– Returns -1 if the character is not found
– Can specify a starting position for finding a match
• LastIndexOf() method returns the position of the last occurrence of the specified character(s) in the string
ASP.NET Programming with C# and SQL Server, First Edition 17
Finding and Extracting Characters and Substrings (cont’d.)
• Substring() method: extracts text from a string starting at the specified position– Can specify the number of characters to be
extracted
• Chars property: retrieves a character by its specified index in a text string– Do not actually refer to it in your code– Use brackets enclosing the index position
– Example: myString[6]
ASP.NET Programming with C# and SQL Server, First Edition 18
ASP.NET Programming with C# and SQL Server, First Edition 19
Table 5-3 Search and extraction methods of the String class
ASP.NET Programming with C# and SQL Server, First Edition 20
Table 5-3 Search and extraction methods of the String class (cont’d.)
Finding and Extracting Characters and Substrings (cont’d.)
Replacing Characters and Substrings
• Replace() method: replaces all instances of a specified character or text within a string– Syntax: string.Replace(oldText, newText)
• Replace() method is case sensitive
ASP.NET Programming with C# and SQL Server, First Edition 21
Converting between Strings and Arrays
• Split() method: splits a string into an indexed array
• Syntax: array=string.Split(separator, [limit]);– separator argument: specifies the character
where the string will be separated into array element– limit argument: sets the maximum length of the
returned array
ASP.NET Programming with C# and SQL Server, First Edition 22
Converting between Strings and Arrays (cont’d.)
• Join() method: combines array elements into a string, separated by specified characters
• Syntax: stringVariable=String.Join
([“separator”], arrayName]);– separator argument: the character(s) that will
separate the contents of each array element in the returned string
• Can use an empty string to prevent elements from being separated by any characters in the new string
ASP.NET Programming with C# and SQL Server, First Edition 23
Comparing Strings• Comparison operators can be used with strings as
well as numbers• String class also provides comparison methods• Equals() method: compares two strings to
determine if they contain the same value
• Syntax: string.Equals(string);• Compare() method: compares two strings
• Syntax: string.Compare(string1,string2)– Returns 0 if string1 and string2 are equivalent– Returns >0 if string2 sorts before string1– Returns -1 if string1 sorts before string2
ASP.NET Programming with C# and SQL Server, First Edition 24
Comparing Strings (cont’d.)
ASP.NET Programming with C# and SQL Server, First Edition 25
Table 5-4 Comparison methods of the String class
Combining Characters and Substrings
• To combine text strings, you can use:– Concatenation operator (+) – Compound assignment operator (+=)– Concat() method of the String class
• Concat() method: creates a new string by combining strings passed as arguments
• Syntax: string.Concat(string1, string2, …)
• Insert() method: inserts text at the specified index position within the string
• Syntax: string.Insert(index,text)
ASP.NET Programming with C# and SQL Server, First Edition 26
Working with Regular Expressions
• Regular expressions: patterns used for matching and manipulating strings according to specified rules– Often used for validating submitted form data
• Must add a using directive to import the System.Text.RegularExpressions namespace to use regular expressions
ASP.NET Programming with C# and SQL Server, First Edition 27
Defining Regular Expressions in C#
• Regex() constructor: part of the Regex class, which represents regular expressions in C#
• Syntax: regExpName = new Regex(“pattern”);
• Use the Match class to determine if a string matches a regular expression pattern– Success property returns true if the regular
expression was found in the string being tested
ASP.NET Programming with C# and SQL Server, First Edition 28
Writing Regular Expression Patterns
• Regular expression patterns contain literal characters and metacharacters
• Metacharacters: special characters that define pattern matching rules in regular expressions
ASP.NET Programming with C# and SQL Server, First Edition 29
Writing Regular Expression Patterns (cont’d)
ASP.NET Programming with C# and SQL Server, First Edition 30
Table 5-5 Regular expression metacharacters
Matching Any Character
• Period (.): – Match any single character – A character must appear in the location where the
period appears in the pattern
• Example: Regex zipPattern = new Regex(“…..”);
ASP.NET Programming with C# and SQL Server, First Edition 31
Matching Characters at the Beginning or End of a String
• Anchor: a pattern that matches the beginning or end of a line– Must use a metacharacter to specify the matching
• ^ metacharacter matches characters at the beginning of a string
• $ metacharacter matches characters at the end of a string
• All literals following the ^ or preceding the $ compose the anchor
• Example: Regex urlProtocol = new Regex(“^http”);
ASP.NET Programming with C# and SQL Server, First Edition 32
Matching Special Characters
• To match any metacharacters as literal values in a regex, precede the character with a backslash to “escape” the metacharacter– Must also precede the pattern string with @ to force
C# to ignore the escaped character
• Example: to ensure that a string contains a periodRegex urlIdentifier = new RegEx(@”\.com”);
ASP.NET Programming with C# and SQL Server, First Edition 33
Specifying Quantity
• Quantifier: a metacharacter that specifies the number of matches desired
• ? specifies that the preceding character in the pattern is optional
• + specifies that one or more of the preceding characters match
• * specifies that zero or more of the preceding characters match
• {n} specifies the precise number of times that a character must repeat
ASP.NET Programming with C# and SQL Server, First Edition 34
Specifying Quantity (cont’d.)
ASP.NET Programming with C# and SQL Server, First Edition 35
Table 5-6 Regular expression quantifiers
Specifying Subexpressions
• Subexpression (or subpattern): a portion of the pattern enclosed in parentheses– Allows you to specify the format and quantities of the
enclosed characters as a group
• Example: regex for a telephone number“^(1-)?(\(.{3}\) )?(.{3})(\-.{4})$”
– Optionally includes 1 and area code, followed by a space
– Includes two groups of numbers, one with 3 and one with 4, separated by a hyphen
ASP.NET Programming with C# and SQL Server, First Edition 36
Defining Character Classes
• Character classes: used in regular expressions to treat multiple characters as a single item– Created by enclosing the characters that make up
the class in square brackets [ ]– Any character in the class represents an alternate
character that would be accepted as a match
• Example: Regex aWord = new Regez(“analy[sz]e”);
• Use a hyphen to specify a range of values– Example: [a-z] is all lowercase letters
ASP.NET Programming with C# and SQL Server, First Edition 37
Defining Character Classes (cont’d.)
• Use the ^ metacharacter before optional characters to be excluded– Example: exclude letters E and G through Z
Regex grade = new Regex(“[^EG-Z]”);
• Can combine ranges in a character class– Example: include all alphanumeric characters
Regex alpha = new Regex(“[0-9a-zA-Z]”);
• Can use special escape characters to represent common types of data:– \w for all alphanumeric characters
ASP.NET Programming with C# and SQL Server, First Edition 38
ASP.NET Programming with C# and SQL Server, First Edition 39
Table 5-7 Character class escape characters
Defining Character Classes (cont’d.)
Matching Multiple Pattern Choices
• Use the Or metacharacter (|) to specify an alternate set of substrings
• Example: check that a Web site domain ends in .com, .org, or .net
Regex url = new Regex
(@”\.(com|org|net)$”);
ASP.NET Programming with C# and SQL Server, First Edition 40
Setting Regular Expression Options
• RegexOptions enumeration: used to set how C# executes regular expressions
• Enumeration: a C# construct containing a set of named constants called an enumerator list
• IgnoreCase enumerator: determines whether to ignore the case of a letter– Passed as a second argument– Example: Regex url = new Regex
(@”\.(com|org|net)$”, RegexOptions.IgnoreCase);
ASP.NET Programming with C# and SQL Server, First Edition 41
Setting Regular Expression Options (cont’d.)
• Use the piping symbol (|) to separate multiple RegexOption enumerators
• IgnorePatternWhitespace enumerator: ignores any white space– Example:
Regex url = new Regex (@”\.(com|org|net)$”, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
ASP.NET Programming with C# and SQL Server, First Edition 42
Using the RegularExpressionValidator Control
• RegularExpressionValidator control: used to programmatically validate a regular expression – ControlToValidate property: field to be validated– ValidationExpression property: the regular
expression pattern to be used
ASP.NET Programming with C# and SQL Server, First Edition 43
Summary
• Parsing: the act of extracting characters or substrings• String class is used to represent all literal strings
and string variables in C#
• Simple Mail Transport Protocol (SMTP): protocol used by most e-mail systems
• Namespace: an abstract container that manages identifiers in a C# program
• MailMessage class: used to create an e-mail object• MailAddress class: used to identify e-mail address
and display name of the sender
ASP.NET Programming with C# and SQL Server, First Edition 44
Summary (cont’d.)• Use ASP.NET Web Site Administration Tool to
administer and configure a Web site• SmtpClient class: allows you to send e-mail
messages with SMTP• Length property of the String class returns the
number of characters in a string• IsNullOrEmpty() method determines if a string
variable contains characters or is empty• String class methods allow changing character
case, trimming spaces, removing characters from start or end of a string, and search and extraction of substrings
ASP.NET Programming with C# and SQL Server, First Edition 45
Summary (cont’d.)
• String class’s Chars property retrieves a character by its index position
• Replace() method replaces text within a string• Split() method splits a string into an indexed
array• Join() method combines array elements into a
string, separated by specified characters• Concat() method creates a new string by
combining strings• Insert() method inserts text at the specified
index position in a stringASP.NET Programming with C# and SQL Server, First Edition 46
Summary (cont’d.)
• Regular expressions are patterns used for matching strings
• Regex constructor is used to define a regular expression
• Regex class contains methods and properties for working with regular expressions
• Match class used to determine if a string matches a regular expression pattern
• Regular expression patterns contain literal characters and metacharacters
ASP.NET Programming with C# and SQL Server, First Edition 47
Summary (cont’d.)
• Use a period to match any single character
• An anchor is a pattern that matches the start or end of a line, specified with the ^ metacharacter
• To match a metacharacter as a literal value, precede it with a backslash
• Quantifiers specify the number of matches desired
• Characters in parentheses are called subexpressions
• Use character classes to treat multiple characters as a single item
ASP.NET Programming with C# and SQL Server, First Edition 48
Summary (cont’d.)
• Separate alternate sets of substrings with the | metacharacter
• Use RegexOptions enumeration to configure how C# executes regular expressions
• Enumeration is a C# construct that contains a set of named constants
• RegularExpressionValidator control is used to programmatically validate a regular expression
ASP.NET Programming with C# and SQL Server, First Edition 49
top related