String in Perl is a sequence of character enclosed within some kinds of quotation marks. Perl string can contain UNICODE, ASCII and escape sequence characters. Perl provides the various function to manipulate the string like any other programming language. Some string functions of Perl are as follows:
- length()
- lc()
- uc()
- index()
- rindex()
length(): This function is used to find the number of characters in a string. This function returns the length of the string. Below are the programs to illustrate this method.
- Example 1:
# Perl program to demonstrate# string length function# stringmy$s="geeksforgeeks";# using length function &# displaying lengthprint(length($s),"\n");chevron_rightfilter_noneOutput:
13
- Example 2:
# Perl program to demonstrate# string length function# stringmy$s="#$%HeLLo CSHARP &+#*";# using length function &# displaying lengthprint(length($s),"\n");chevron_rightfilter_noneOutput:
19
lc(): This function return the lower case version of a string. Below are the programs to illustrate this method.
- Example 1:
# Perl program to demonstrate# string lc function# stringmy$s="GEEKSFORGEEKS\n";# using lc function &# displaying resultprint("To lower case: ");print(lc($s),"\n");chevron_rightfilter_noneOutput:
To lower case: geeksforgeeks
- Example 2:
# Perl program to demonstrate# string lc function# stringmy$s="GEEKS\n";# using lc function &# displaying resultprint("To lower case: ");print(lc($s),"\n");chevron_rightfilter_noneOutput:
To lower case: geeks
uc(): This function return the upper case version of a string. Below are the programs to illustrate this method.
- Example 1:
# Perl program to demonstrate# string uc function# stringmy$s="geeksforgeeks";# using uc function &# displaying resultprint("To Upper Case: ");print(uc($s),"\n");chevron_rightfilter_noneOutput:
To Upper Case: GEEKSFORGEEKS
- Example 2:
# Perl program to demonstrate# string uc function# stringmy$s="GeekS\n";# using uc function &# displaying resultprint("To Upper Case: ");print(uc($s),"\n");chevron_rightfilter_noneOutput:
To Upper Case: GEEKS
index(): This method will search a substring from a specified position in a string and returns the position of the first occurrence of the substring in the string. If the position is omitted, it will search from the beginning of the string. This method will take the two parameters i.e. the original string and the substring that has to be searched.
Example :
# Perl Program to illustrate # the index() function # !/usr/bin/perl use warnings; use strict; # string my $st = "GeeksforGeeks\n"; # substring my $subs = "for"; # using index function my $r = index($st, $subs); # displaying result print(qq\The substring $subs found at position $r in string $st\); |
Output:
The substring for found at position 5 in string GeeksforGeeks
rindex() This function is same as index() except it returns the last occurrence of text in a string. Also, a third parameter can be given which returns position before or at that location. It searches from the end of the string instead of from the beginning. Below are the programs to illustrate this method.
Example 1:
# Perl Program to illustrate # the rindex() function # !/usr/bin/perl use warnings; use strict; # string my $st = "GeeksforGeeks\n"; # substring my $subs = "for"; # using rindex function my $r = rindex($st, $subs); # displaying result print(qq\The substring $subs found at position $r in string $st\); |
Output:
The substring for found at position 5 in string GeeksforGeeks
Example 2:
# Perl Program to illustrate # the rindex() function with # three parameters # !/usr/bin/perl # using rindex() function $p = rindex("GeeksForGFGGeeksgeeksforGFG", "GFG"); print "Founded position of GFG $p\n"; # Use the first position found # as the offset to the next search. # The length of the target string # is subtracted from the offset # to save time. $p = rindex("GeeksForGFGGeeksgeeksforGFG", "GFG", $p-7); print "Founded position of GFG $p\n"; |
Output:
Founded position of GFG 24 Founded position of GFG 8
Recommended Posts:
- Perl | rindex() Function
- Perl | Useful String functions
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- Perl | Subroutines or Functions
- Perl | Subroutines or Functions | Set - 2
- Perl | List Functions
- Perl | Useful Array functions
- Perl | Useful Math functions
- Perl | Useful Hash functions
- Perl | Useful File-handling functions
- Perl | File I/O Functions
- Perl - Difference between Functions and Subroutines
- Perl | index() Function
- Perl | length() Function
- Perl | Math::BigInt->length() method
- Perl | Automatic String to Number Conversion or Casting
- Perl | String Operators
- Perl | Useful String Operators
- Perl | Extract IP Address from a String using Regex
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

