Perl tutorial: 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.

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 jumped from the green tree";

  say index $str, "cat";             # 10
  say index $str, "dog";             # -1

Because the "cat" starts on the 10th place. There is no "dog" in that sentence and index indicates a failure by returning -1. 0 is used when the second string is a prefix of the first string as in the following example:


  say index $str, "The";             # 0
  say index $str, "the";             # 26

OTOH index is looking for exact matches so case also matters. Hence "the" and "The" are different.

index() looks for strings not for words so even "e " can be looked up. index() can also have a 3rd parameter that indicates the location where to start searching. So as we found "e " to be index 2 of the first string if we now start searching from the 3rd place then we will find another occurrence of "e ". If there is one:


  say index $str, "e ";              # 2
  say index $str, "e ", 3;           # 28
  say index $str, "e", 3;            # 18

Looking for "e" without a space will yield a different result.

Lastly, there is another function called rindex


  say rindex $str, "e";              # 39
  say rindex $str, "e", 38;          # 38
  say rindex $str, "e", 37;          # 33

the full example is


  use strict;
  use warnings;
  use 5.010;

  my $str = "The black cat jumped from the green tree";

  say index $str, "cat";             # 10
  say index $str, "dog";             # -1

  say index $str, "The";             # 0
  say index $str, "the";             # 26

  say index $str, "e ";              # 2
  say index $str, "e ", 3;           # 28
  say index $str, "e", 5;            # 18

  say rindex $str, "e";              # 39
  say rindex $str, "e", 38;          # 38
  say rindex $str, "e", 37;          # 33


I think the most interesting function I am going to show now is substr. It is basically the opposite of index(). index() will tell you where is a string while substr will give you the substring at a given locations. Normally substr gets 3 parameters. The first one is a string. The second is a 0-based location, also called 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.


  say substr $str, 4, -11;                    # black cat climbed the

The length parameter can also be a negative number in which 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.


  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 4th place till the end of the string.


  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 the from there. It is the same as having length($str)-4 in the offset.

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 behave the same way but it will also change the content of the original string!

The return value of substr() is 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 $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 mark the word climbed in the original string and that is the return value of substr() but because of the 4th parameter, the original string was changed.

The full script:


  use strict;
  use warnings;
  use 5.010;

  my $str = "The black cat climbed the green tree";

  say substr $str, 4, 5;                      # black

  say substr $str, 4, -11;                    # black cat climbed the


  say substr $str, 14;                        # climbed the green tree

  say substr $str, -4;                        # tree
  say substr $str, -4, 2;                     # tr

  my $z = substr $str, 14, 7, "jumped from";
  say $z;                                     # climbed
  say $str;                 # The black cat jumped from the green tree



Perl tutorial and video course

For further articles see the Beginner Perl Maven tutorial book and video course.


In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
blog comments powered by Disqus
Online courses:


Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed