String functions: length, lc, uc, index, substr
In this part of the Perl Tutorial we are going to learn about some of the functions Perl provides for manipulating strings.
lc, uc, length
There are a number of simple functions such as lc and uc to return the lower case and upper case versions of the original string respectively. Then there is length to return the number of characters in the given string.
See the following example:
use strict; use warnings; use 5.010; my $str = 'HeLlo'; say lc $str; # hello say uc $str; # HELLO say length $str; # 5
Then there is the index function. This function will get two strings and return the location of the second string within the first string.
use strict; use warnings; use 5.010; my $str = "The black cat climbed the green tree"; say index $str, 'cat'; # 10 say index $str, 'dog'; # -1 say index $str, "The"; # 0 say index $str, "the"; # 22
The first call to index returned 10, because the string "cat" starts on the 10th character. The second call to index returned -1 indicating there is no "dog" in that sentence.
The 3rd call shows that index returns 0 when the second string is a prefix of the first string.
The 4th example shows that index is looking for exact matches so case also matters. Hence "the" and "The" are different.
index() looks for strings and not just for words, so even the string "e " can be looked up:
my $str = "The black cat climbed the green tree"; say index $str, "e "; # 2
index() can also have a 3rd parameter that indicates the location where to start searching. So as we found "e " to start at the 2nd character of the first string, we could try to search starting from the 3rd place to see if there is another occurrence of "e ":
my $str = "The black cat climbed the green tree"; say index $str, "e "; # 2 say index $str, "e ", 3; # 24 say index $str, "e", 3; # 19
Looking for "e" without a space will yield a different result.
Lastly, there is another function called rindex (right index) that will start searching from the right hand side of the string:
my $str = "The black cat climbed the green tree"; say rindex $str, "e"; # 35 say rindex $str, "e", 34; # 34 say rindex $str, "e", 33; # 29
I think the most interesting function in this article is substr. It is basically the opposite of index(). While index() will tell you where is a given string, substr will give you the substring at a given locations. Normally substr gets 3 parameters. The first one is the string. The second is a 0-based location, also called the offset, and the third is the length of the substring we would like to get.
use strict; use warnings; use 5.010; my $str = "The black cat climbed the green tree"; say substr $str, 4, 5; # black
substr is 0 based so the character at the offset 4 is the letter b.
my $str = "The black cat climbed the green tree"; say substr $str, 4, -11; # black cat climbed the
The 3rd parameter (the length) can also be a negative number. In that case it tells us the number of characters from the right hand side of the original string that should NOT be included. So the above means: count 4 from the left, 11 from the right, return what is between.
my $str = "The black cat climbed the green tree"; say substr $str, 14; # climbed the green tree
You can also leave out the 3rd (length) parameter which will mean: return all the characters starting from the 14th place till the end of the string.
my $str = "The black cat climbed the green tree"; say substr $str, -4; # tree say substr $str, -4, 2; # tr
We can also use a negative number in the offset, which will mean: count 4 from the right and start from there. It is the same as having length($str)-4 in the offset.
Replacing part of a string
The last example is a bit funky. So far in every case substr returned the substring and left the original string intact. In this example, the return value of substr will still behave the same way, but substr will also change the content of the original string!
The return value of substr() is always determined by the first 3 parameters, but in this case substr has a 4th parameter. That is a string that will replace the selected substring in the original string.
my $str = "The black cat climbed the green tree"; my $z = substr $str, 14, 7, "jumped from"; say $z; # climbed say $str; # The black cat jumped from the green tree
So substr $str, 14, 7, "jumped from" returns the word climbed, but because of the 4th parameter, the original string was changed.
There is also an lvalue version of this example, but only look there if you are brave.