ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
CSCI 6962: Server-side Design and Programming Input Validation and Error Handling.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Asp.NET Core Vaidation Controls. Slide 2 ASP.NET Validation Controls (Introduction) The ASP.NET validation controls can be used to validate data on the.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Tutorial 14 Working with Forms and Regular Expressions.
JavaScript, Fourth Edition
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Third Edition
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
CST JavaScript Validating Form Data with JavaScript.
Chapter 5 new The Do…Loop Statement
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
1.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Chapter 9 Creating Formulas that Manipulate Text Microsoft Office Excel 2003.
XP Tutorial 14 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Chapter 4 Handling User Input PHP Programming with MySQL 2nd Edition
Tutorial 14 Working with Forms and Regular Expressions.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Chapter 2 Basic SQL SELECT Statements Oracle 10g: SQL.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Regular Expressions in.NET Ashraya R. Mathur CS NET Security.
Web Application and Development Digital Media Department Unit Credit Value : 4 Essential Learning time : 120 hours Digital Media.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Characters In Java, single characters are represented.
Chapter 8 Cookies And Security JavaScript, Third Edition.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
Chapter 8: Arrays.
BY Sandeep Kumar Gampa.. What is Regular Expression? Regex in.NET Regex Language Elements Examples Regular Expression API How to Test regex in.NET Conclusion.
Chapter 8: Manipulating Strings
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Chapter 7: Characters, Strings, and the StringBuilder.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Unit 11 –Reglar Expressions Instructor: Brent Presley.
7 Copyright © 2009, Oracle. All rights reserved. Regular Expression Support.
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
8 1 String Manipulation CGI/Perl Programming By Diane Zak.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
Microsoft Visual Basic 2008: Reloaded Third Edition
The Selection Structure
String String Builder.
Chapter 19 PHP Part II Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Introduction to Scripting
Working with Forms and Regular Expressions
Primitive Types Vs. Reference Types, Strings, Enumerations
Topics Basic String Operations String Slicing
Introduction to Computer Science
Visual Programming COMP-315
Topics Basic String Operations String Slicing
CIS 136 Building Mobile Apps
Topics Basic String Operations String Slicing
Presentation transcript:

ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#

Objectives In this chapter, you will: Manipulate strings Parse strings Work with regular expressions ASP.NET Programming with C# and SQL Server, First Edition2

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 Edition3

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 Edition4

Manipulating Strings (cont’d.) String class: contains methods and properties for manipulating strings System.Net.Mail namespace: contains classes for sending to an SMTP server Simple Mail Transfer Protocol (SMTP): a protocol used by most 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 name ASP.NET Programming with C# and SQL Server, First Edition5

Manipulating Strings processing directive: used to make a namespace available for use in an ASP.NET Web page Example: 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 Edition6

Manipulating Strings (cont’d.) MailMessage class: used to create an object containing sender, recipient, subject, and body MailMessage Object = new MailMessage(); MailAddress class: used to identify the address and display name of the sender or recipient MailAddress from = new MailAddress “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 Edition7

Manipulating Strings (cont’d.) ASP.NET Web Site Administration Tool: used to administer and configure a Web site –Configure SMTP settings to send 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 Edition8

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 Edition9

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 Edition10

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(“ ” + myName.toUpper() + “ ”); Original value of the variable is not changed ASP.NET Programming with C# and SQL Server, First Edition11

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 Edition12 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 Edition13

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 Edition14 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 Edition15

Finding and Extracting Characters and Substrings It is common to extract characters and substrings from strings –Example: extract the name portion of an 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 Edition16

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 Edition17

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 Edition18

ASP.NET Programming with C# and SQL Server, First Edition19 Table 5-3 Search and extraction methods of the String class

ASP.NET Programming with C# and SQL Server, First Edition20 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 Edition21

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 Edition22

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 Edition23

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 Edition24

Comparing Strings (cont’d.) ASP.NET Programming with C# and SQL Server, First Edition25 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 Edition26

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 Edition27

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 Edition28

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 Edition29

Writing Regular Expression Patterns (cont’d) ASP.NET Programming with C# and SQL Server, First Edition30 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 Edition31

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 Edition32

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 to force C# to ignore the escaped character Example: to ensure that a string contains a period Regex urlIdentifier = new ASP.NET Programming with C# and SQL Server, First Edition33

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 Edition34

Specifying Quantity (cont’d.)‏ ASP.NET Programming with C# and SQL Server, First Edition35 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 Edition36

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 Edition37

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 Edition38

ASP.NET Programming with C# and SQL Server, First Edition39 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 ASP.NET Programming with C# and SQL Server, First Edition40

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 RegexOptions.IgnoreCase); ASP.NET Programming with C# and SQL Server, First Edition41

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 RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); ASP.NET Programming with C# and SQL Server, First Edition42

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 Edition43

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 systems Namespace: an abstract container that manages identifiers in a C# program MailMessage class: used to create an object MailAddress class: used to identify address and display name of the sender ASP.NET Programming with C# and SQL Server, First Edition44

Summary (cont’d.)‏ Use ASP.NET Web Site Administration Tool to administer and configure a Web site SmtpClient class: allows you to send 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 Edition45

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 string ASP.NET Programming with C# and SQL Server, First Edition46

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 Edition47

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 Edition48

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 Edition49