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
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
.
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.
length
FunctionThe length
function returns the number of characters in a string.
my $string = "Hello, World!"; print length($string); # Outputs: 13
lc
and uc
FunctionsThe 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!
index
FunctionThe 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)
rindex
FunctionThe 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)
String Inspection: Quickly determine the length of user input using the length
function.
Case-Insensitive Searching: Convert both the main string and the search string to lowercase using lc
before searching to make the search case-insensitive.
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.
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.
Using length()
in Perl for string length:
length()
function.my $string = "Hello, Perl!"; my $length = length($string); print "Length of the string: $length\n";
lc()
and uc()
functions in Perl:
lc()
) or uppercase (uc()
).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";
Converting case with lc
and uc
in Perl:
lc
and uc
.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";
Searching for substrings with index()
in Perl:
index()
.my $text = "Perl is powerful!"; my $position = index($text, "is"); print "Substring 'is' found at position: $position\n";
Right-to-left substring search with rindex()
in Perl:
rindex()
.my $text = "Perl is powerful and Perl is versatile!"; my $last_position = rindex($text, "Perl"); print "Last position of 'Perl': $last_position\n";
Perl string manipulation functions:
my $original = " Trim me "; my $trimmed = trim($original); my $reversed = reverse($original); print "Original: '$original'\n"; print "Trimmed: '$trimmed'\n"; print "Reversed: '$reversed'\n";
Examples of length
, lc
, uc
, index
, rindex
in Perl:
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";
String manipulation tips and tricks in Perl:
my $original = " Trim me "; my $trimmed = trim($original); my $reversed = reverse($original); print "Original: '$original'\n"; print "Trimmed: '$trimmed'\n"; print "Reversed: '$reversed'\n";