String Functions

There is a close relationship between an operator and a function. For example, Perl uses ** for exponentiation, whereas some languages use the pow function to achieve the same result. Likewise, Perl uses the % symbol for the remaindering operation, while other languages use the MOD function to achieve the same result. Thus, in this section we present some simple but useful functions that you can use on scalars.

The length Function

The length function returns the length of a scalar.

$string="This	string
consists
of a few
lines
";
$len = length($string);                 #    length = 36

Remember that the sequence represents the tab character and the sequence represents the newline character. Thus, each of these sequences counts as a single character in the code above.

The index and rindex Functions

The index function returns the first position (zero-based) within a string where a second string is found or –1 if the second string is not found. The index function can also have a third argument: an integer specifying how much to skip in the first string before you start indexing.

$s = "if you don't succeed, try try again";
$pos1 = index($s,"try");                  # pos = 22
$pos2 = index($s, "Try");                 # pos = -1
$pos3 = index($s, "try", $pos1 + 1);      # pos = 26

The rindex function behaves like index but returns the index of the last occurrence of the second string.

$s = "He had had had where she had had had had";
$pos = rindex($s, "had");                 # pos = 37

The substr Function

The substr function returns a portion of a string starting at a specified position for a specified length. If the length is not given, the entire string from the specified beginning position is returned.

$var = "HOME=/users/home";
$where = index($var, "=");
$name = substr($var, 0, $where);
$val = substr($var, $where + 1);         # HOME
print "$name
$val
";                   # /users/home

The substr function may also be used to alter a string. That is, it can be used on the left-hand side of an assignment. See the folder Substr.


% type substr.pl
#
#   substr.pl
#
$test = "Java is inferior";
substr($test, 8, 5) = "super";
print "$test
";
substr($test, 0, 4) = "Perl";
print "$test
";
substr($test, 0, 0) = "My My: ";
print "$test
";
%

The first substr above changes infer to super. Next, Java is changed to Perl. And finally, My My: is prepended to the phrase.

% perl substr.pl
Java is superior
Perl is superior
My My: Perl is superior
%

Functions Dealing with Case

The functions lc and uc return the lowercase and uppercase versions of the argument strings respectively. The functions lcfirst and ucfirst return the argument strings with only the first character changed to the appropriate case. See the folder Cases.


% type cases.pl
#
#   cases.pl
#
print "Enter a string: ";
$string = <STDIN>;
print "LOWER CASE: ", lc($string);
print "FIRST CHAR: ", lcfirst($string), "
";
print "UPPER CASE: ", uc($string);
print "FIRST CHAR: ", ucfirst($string);
% perl cases.pl
Enter a string: a SIMPLE string
LOWER CASE: a simple string
FIRST CHAR: a simple string
UPPER CASE: A SIMPLE STRING
FIRST CHAR: A simple string
%

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.119.17