分类:
2008-04-13 22:01:46
|
Comments
When it comes to altering the case of a string, Perl makes it easy with four built-in functions. Two of them a illustated previously: the uc() function uppercase all the characters in a string, it returns an uppercased version of EXPR, and it is the internal function implementing the \U escape in double-quoted strings. The lc() function lowercases all the characters in a string. it returns a lowercased version of EXPR. This is the internal function implementing the \L
escape in double-quoted strings.
For more precise control, consider the ucfirst() function, which capitalizes the first character of a string, and the lcfirst() function, which lowercases the first character of a string. ucfirst() is the internal function implementing the \u escape in double-quoted strings, lcfirst() is the internal function implementing the \l escape in double-quoted strings. Here is an example:
|
1.2 Checking for Empty String Values
Problem
You want to check if a string value contains valid characters.
Solution
Use a combination of Perl's defined() and our own trim()functions:
|
Comments
You'll use this often when working with form data, to see if a required form field contains valid data or not. The basic technique is simple: use defined() to verify that the string variable exists, then use our trim() function to trim whitespace from the edges and equate it to an empty string. If the test returns true, it's confirmation that the string cotains no value.
1.3 Removing Characters from the Ends of a String
Problem
You want to remove the first/last n characters from a string.
Solution
Use the substr() function to slice off the required number of characters from the beginning or end of the string.
|
1.4 Removing Whitespace from Strings
Problem
You want to remove all or some whitespace form a string, or compress multiple spaces in a string.
Solution
|
|
1.6 Repeating Strings
Problem
You want to repeat a string n times.
Solution
Use the x operator.
|
1.7 Truncating Strings
Problem
You want to truncate a long string to a particular length, and replace the truncated characters with a custom placeholder-for example, with ellipses.
Solution
Use substr()
|
1.8 Converting Between ASCII Characters and Codes
Problem
You want retrive the ASCII code corresponding to a partivular character, or vice versa.
Solution
Use the ord() function to get the ASCII code for a character, use the chr() function to get the character corresponding to an ASCII code.
|
1.9 Splitting Strings into Smaller Chunks
Problem
You want to break up a long string into smaller segments, each of a fixed size.
Solution
use regex
|
1.10 Comparing Strings for Similarity
Problem
You want to compare two strings to see if they sound similar.
Solution
Use Text::Metahphone or Text::DoubleMetaphone
|
1.11 Parsing Comma-Separated Lists
Problem
You want to extract the individual elements of a comma-separated list.
Solution
Use split()
1.12 Parsing URLs
Problem
You want to extract the protocol, domain name, path, or other significant component of a URL.
Solution
URI::Split
wait wait wait ...
----to be continued----