Perl | String functions (length, lc, uc, index, rindex)
Last Updated :
26 Jun, 2019
Improve
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:
Perl
Output:
Perl
Output:
Perl
Output:
- length()
- lc()
- uc()
- index()
- rindex()
- Example 1:
Perl # Perl program to demonstrate # string length function # string my $s = "geeksforgeeks"; # using length function & # displaying length print(length($s),"\n");
13
- Example 2:
Perl # Perl program to demonstrate # string length function # string my $s = "#$%HeLLo CSHARP &+#*"; # using length function & # displaying length print(length($s),"\n");
19
- Example 1:
Perl # Perl program to demonstrate # string lc function # string my $s = "GEEKSFORGEEKS\n"; # using lc function & # displaying result print("To lower case: "); print(lc($s),"\n");
To lower case: geeksforgeeks
- Example 2:
Perl # Perl program to demonstrate # string lc function # string my $s = "GEEKS\n"; # using lc function & # displaying result print("To lower case: "); print(lc($s),"\n");
To lower case: geeks
- Example 1:
Perl # Perl program to demonstrate # string uc function # string my $s = "geeksforgeeks"; # using uc function & # displaying result print("To Upper Case: "); print(uc($s),"\n");
To Upper Case: GEEKSFORGEEKS
- Example 2:
Perl # Perl program to demonstrate # string uc function # string my $s = "GeekS\n"; # using uc function & # displaying result print("To Upper Case: "); print(uc($s),"\n");
To Upper Case: GEEKS
# 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\);
The substring for found at position 5 in string GeeksforGeeksrindex() 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\);
The substring for found at position 5 in string GeeksforGeeksExample 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";
Founded position of GFG 24 Founded position of GFG 8