Perl Tutorial

Fundamentals

Input and Output

Control Flow

Arrays and Lists

Hash

Scalars

Strings

Object Oriented Programming in Perl

Subroutines

Regular Expressions

File Handling

Context Sensitivity

CGI Programming

Misc

String functions (length, lc, uc, index, rindex) in Perl

String Functions in Perl Tutorial

In Perl, strings are a fundamental datatype, and various built-in functions allow you to manipulate and inspect them. This tutorial covers some of the essential string functions: length, lc, uc, index, and rindex.

1. Introduction

String functions provide an array of utilities that can be utilized to perform operations on strings without the need for regex or other more complex tools.

2. length Function

The length function returns the number of characters in a string.

my $string = "Hello, World!";
print length($string);  # Outputs: 13

3. lc and uc Functions

The lc function converts a string to lowercase, while uc converts a string to uppercase.

my $string = "Hello, World!";

print lc($string);  # Outputs: hello, world!
print uc($string);  # Outputs: HELLO, WORLD!

4. index Function

The index function returns the position of the first occurrence of a substring within a string. If the substring is not found, it returns -1.

my $string = "Hello, World!";

print index($string, "World");  # Outputs: 7
print index($string, "world");  # Outputs: -1 (because the search is case-sensitive)

You can also specify a start position for the search:

my $string = "apple apple";
print index($string, "apple", 2);  # Outputs: 6 (starts searching from the 2nd character)

5. rindex Function

The rindex function is similar to index, but it starts its search from the end of the string.

my $string = "apple apple";
print rindex($string, "apple");  # Outputs: 6 (finds the last occurrence)

Like index, you can also provide a start position for rindex:

my $string = "apple apple apple";
print rindex($string, "apple", 10);  # Outputs: 6 (starts searching from the 10th character and goes backward)

6. Practical Use Cases

  1. String Inspection: Quickly determine the length of user input using the length function.

  2. Case-Insensitive Searching: Convert both the main string and the search string to lowercase using lc before searching to make the search case-insensitive.

  3. Finding Substrings: Use index and rindex to locate the positions of particular patterns or words within a larger text, such as finding links within a web page source.

7. Summary

Perl offers a rich set of built-in functions for string manipulation:

  • length provides the character count of a string.
  • lc and uc are used for case conversion.
  • index and rindex help in locating substrings from the beginning or the end, respectively.

Understanding these functions provides a foundation for effective string handling and text processing in Perl.

  1. Using length() in Perl for string length:

    • Description: Determining the length of a string using the length() function.
    • Code Example:
      my $string = "Hello, Perl!";
      my $length = length($string);
      print "Length of the string: $length\n";
      
  2. lc() and uc() functions in Perl:

    • Description: Converting a string to lowercase (lc()) or uppercase (uc()).
    • Code Example:
      my $mixed_case = "MiXeD CaSe";
      my $lowercase = lc($mixed_case);
      my $uppercase = uc($mixed_case);
      
      print "Original: $mixed_case\n";
      print "Lowercase: $lowercase\n";
      print "Uppercase: $uppercase\n";
      
  3. Converting case with lc and uc in Perl:

    • Description: Demonstrating case conversion using lc and uc.
    • Code Example:
      my $mixed_case = "HeLLo WoRLD";
      my $lowercase = lc($mixed_case);
      my $uppercase = uc($mixed_case);
      
      print "Original: $mixed_case\n";
      print "Lowercase: $lowercase\n";
      print "Uppercase: $uppercase\n";
      
  4. Searching for substrings with index() in Perl:

    • Description: Finding the position of a substring within a string using index().
    • Code Example:
      my $text = "Perl is powerful!";
      my $position = index($text, "is");
      print "Substring 'is' found at position: $position\n";
      
  5. Right-to-left substring search with rindex() in Perl:

    • Description: Finding the last position of a substring using rindex().
    • Code Example:
      my $text = "Perl is powerful and Perl is versatile!";
      my $last_position = rindex($text, "Perl");
      print "Last position of 'Perl': $last_position\n";
      
  6. Perl string manipulation functions:

    • Description: Utilizing various string manipulation functions in Perl.
    • Code Example:
      my $original = "   Trim me   ";
      my $trimmed =  trim($original);
      my $reversed = reverse($original);
      
      print "Original: '$original'\n";
      print "Trimmed: '$trimmed'\n";
      print "Reversed: '$reversed'\n";
      
  7. Examples of length, lc, uc, index, rindex in Perl:

    • Description: Combining multiple string manipulation functions in a single example.
    • Code Example:
      my $phrase = "Perl is Awesome!";
      my $length = length($phrase);
      my $lowercase = lc($phrase);
      my $uppercase = uc($phrase);
      my $position = index($phrase, "is");
      my $last_position = rindex($phrase, "e");
      
      print "Original: '$phrase'\n";
      print "Length: $length\n";
      print "Lowercase: '$lowercase'\n";
      print "Uppercase: '$uppercase'\n";
      print "Position of 'is': $position\n";
      print "Last position of 'e': $last_position\n";
      
  8. String manipulation tips and tricks in Perl:

    • Description: Showcasing tips and tricks for effective string manipulation.
    • Code Example:
      my $original = "   Trim me   ";
      my $trimmed =  trim($original);
      my $reversed = reverse($original);
      
      print "Original: '$original'\n";
      print "Trimmed: '$trimmed'\n";
      print "Reversed: '$reversed'\n";