8 String Manipulation CGI/Perl Programming By Diane Zak 1 8 Objectives • In this chapter, you will: • Convert a string to uppercase letters using the uc function • Convert a string to lowercase letters using the lc function • Return, replace, or insert text using the substr function • Use the index function to locate a string within another string 2 8 Objectives • In this chapter, you will: • Replace text using the transliteration operator • Use the binding operators • Perform pattern matching using the matching operator • Include metacharacters in a search pattern • Replace text using the substitution operator 3 8 Introduction • Often, a script will need to manipulate data it receives from a server – Formatting – Conversion – Other tasks 4 8 The uc and lc functions • String comparisons are case sensitive • Instead of checking for multiple cases within a condition statement, can use functions to temporarily change the case 5 8 The uc and lc functions • uc function: – Changes string to all upper case – Syntax: • uc (string) – Example: if (uc($answer) eq “Y”) { statement(s) to process if condition is true } Results: Temporarily converts the contents of the $answer variable to uppercase, then compares the uppercase value to “Y” 6 8 The uc and lc functions • lc function: – Changes string to all lower case – Syntax: • lc (string) Results: – Example: Temporarily while (lc($item) ne “done”) { converts the contents of the statement(s) to process $item variable to lowercase, while condition is true then compares } the lowercase value to “done” 7 8 The uc and lc functions • Both functions only affect alphabetic characters • The parentheses around the variable name is optional • Can also use ucfirst function – Converts only first character in string to uppercase – Can use with lc to capitalize first letter of string and have the remaining letters in lowercase 8 8 The substr function • Can use substr function to: – Return a portion of a string within a string – Replace one or more characters in a string – Insert one or more characters within a string 9 8 The substr function • Using substr to return characters: – Syntax: • substr (string, start, length) – Return length number of characters from string beginning at start – If no length, return all characters from start to end of string – Examples: $name = “John Smitty”; $first = substr ($name, 0, 4); Assigns John to $first $name = “John Smitty”; $last = substr ($name, 5, 6); $last = substr ($name, 5); Both substr statements assign Smitty to $last 10 8 The substr function • Using substr to replace characters: – Syntax: • substr (string, start, length) = replacementstring – Replace length number of characters in string with replacementstring, beginning at start in string – If no length, replaces all characters from start to end of string – Examples: $name = “Pat Jeffrey”; Replaces Pat with substr ($name, 0, 3) = “Patty”; Patty. $name now stores Patty Jeffrey. $name = “Pat Jeffrey”; substr ($name, 4) = “Carr”; Replaces Jeffrey with Carr. $name 11 now stores Pat Carr. 8 The substr function • Using substr to insert characters: – Syntax: • substr (string, start, 0) = insertionstring – Insert insertionstring within string, beginning at position start in string. – Examples: $price = “45”; substr ($price, 0, 0) = “\$”; Inserts a dollar sign in position 0 of $price variable, which now stores $45 $location = “Chicago IL”; substr ($location, 7, 0) = “, “; Inserts a comma in position 7 of $location variable, which now12 stores Chicago, IL. 8 The index function • Can be used to search a string to determine if it contains another string • Syntax: – index (string, substring, start) – Returns the position of the first occurrence of substring within the string beginning with start position in string. • If substring is not found, function returns –1 • Otherwise, returns the beginning position of the first occurrence of substring – If start is omitted, the search begins at 13 8 The index function 14 The Transliteration and “Contains” Operators 8 • Transliteration operator: – Also referred to as the tr operator or the tr/// operator – Can be used to replace a character in a string – Syntax: • string =~ tr/searchlist/replacementlist/modifier – searchlist – character(s) to search for – replacementlist – optional. character(s) to replace character(s) listed in searchlist – modifier – optional. »d »c »s Delete found but unreplaced characters Complement the searchlist Squash duplicate replaced characters15to a single character The Transliteration and “Contains” Operators 8 • “contains” operator: – Also referred to as a binding operator – Represented by =~ – Used with tr/// operator • Tells tr/// operator to search string to determine if it contains any of the characters in searchlist 16 The Transliteration and “Contains” Operators 8 17 The Transliteration and “Contains” Operators 8 18 8 The Match Operator • Can be used to determine if a string is contained within another string – Like the index function – Referred to as the m operator or the m/// operator – Can use the “contains” operator (=~) or the “does not contain” operator (!~) 19 The Match Operator 8 20 Using Metacharacters in a Search Pattern 8 21 Using Metacharacters in a Search Pattern 8 22 Using Metacharacters in a Search Pattern 8 • Metacharacters and the m/// operator can be used to verify an email address: 23 The Substitution Operator 8 • Can be used to: – Replace one or more characters in a string – Remove one or more characters from a string – Also referred to as the s operator or the s/// operator 24 The Substitution Operator 8 25 8 The International Coffees Form and Script 26 8 The International Coffees Form and Script 27 8 Summary • The uc function temporarily converts the letters in a string to uppercase. • The lc function temporarily converts the letters in a string to lowercase. • You can use the substr function to: – Return portion of a string contained in another string – Replace character(s) in a string – Insert character(s) into a string 28 Summary 8 • The index function to determine if string contains substring – Returns position number of beginning of substring If substring is not within string, returns –1 • The tr/// (transliteration) operator can replace a character in a string, or delete a character from a string. • The m// (match) operator can determine if a string is contained in another string. – Returns true or false – If assigned to an array, returns list of values that match searchpattern 29 8 Summary • The searchpattern entered in the m// and s/// operators can include metacharacters: – – – – Match Match Match Match single character(s) whitespace character(s) boundary character(s) repeated characters • The s/// (substitution) operator can be used to replace or delete character(s) in a string. 30