Perl String

Strings are an essential part of the Perl language. They are scalar variables, so they start with ($) sign. A string can be defined within a single quote (') or double quote (").

String Operators

The operators make it easy to manipulate a string in different ways. There are two types of string operators:

  • Concatenation (.)
  • Repetition (x)

Concatenation Operator

Perl strings are concatenated with a (.) sign instead of (+) sign.

Example
snippet
$firstName = "Christian";
$lastName = "Grey";
$fullName = $firstName . " " . $lastName;
print "$fullName\n";
Output
Christian Grey

Repeitition Operator

Perl strings can be repeated a number of times with (x) variable.

Example
snippet
$text = "Thank You ";
$output = $text x 3;
print "$output\n";
Output
Thank You Thank You Thank You

Initializing and Declaring a String

In Perl, to declare a string use my keyword before variable name.

Syntax

A string can be initialised and declared with the following syntax:

my $variableName = "";

In this example, we have shown how to initialize and declare a string. We have printed several strings together by the following ways:

  • Join strings using a dot (.) operator.
  • Supply strings as separate arguments.
  • Embed strings in a bigger string.

We have shown all the three methods to print the output.

Example
snippet
use strict;
use warnings;
# Declaring and initializing a string.
my $msg1 = "Welcome at rookienerd.";
my $msg2 = "This is our Perl Tutorial.";
#printing using . operator.
print $msg1 . "" . $msg2. "\n";
#print as separate arguments.
print $msg1, "",$msg2, "\n";
#embedd string in a bigger string.
print "$msg1$msg2\n";
Output
Welcome at rookienerd. This is our Perl Tutorial. Welcome at rookienerd. This is our Perl Tutorial. Welcome at rookienerd. This is our Perl Tutorial.

Formatting Characters in string

Character Description
\a Bell
\b Gives a backspace
\cX Control the characters. X is a character.
\e escape next character
\E it ends \u, \l and \q function
\f Gives formfedd to the string
\l Transformation of only next letter into lower case.
\L Transformation of all letters into lower case.
\n Begins next line from a new line
\0nn Octal format numbers are created
\Q Do not match the pattern
\r Gives a carriage return
\t Gives a tab to the string
\u Transformation of only next letter into lower case.
\U Transformation of letters into uppercase.
\xnn Hexadecimal format numbers are created

Single quote Vs Double quote String

Strings can be placed within a single quote (') or double quote (") but they have little different behavior.

Example
snippet
my $user = 'Ana';
print 'Hello $user, welcome at our site.\n';
print "\n";
my $user = 'Ana';
my $day  = "today";
print "Hello $user, welcome at our site $day.\n";
Output
Hello $user, welcome at our site.\n Hello Ana, welcome at our site today.

In single quote, all the characters are interpreted as it is.

Double quote provides interpolation. Means other variables present inside the string will represent their values. Escape characters will be replaced by their values like '\n' will display a new line.

substr()

Perl substr() Example

The substr() function is used to truncate the strings. We need to provide an offset string. String will be truncated till the provided offset value.

Mentioning length with the offset will print the string after offset value and till the mentioned length.

If you provide a new string with offset and length, it will replace the string after offset till the length value.

Example
snippet
use strict;
use warnings;
# Original string
my $originalstring = "Our site rookienerd provides all type of tutorials";
print "$originalstring\n";
# Offset of 4
my $offset = substr($originalstring, 4);
print "$offset\n";
# Offset of 4, length 15
my $offsetlength = substr($originalstring, 4, 15);
print "$offsetlength\n";
# Replacing length with the new string
my $replacing = substr($originalstring, 4, 15, "one and only site");
print "$originalstring\n";
Output
Our site rookienerd provides all type of tutorials site "rookienerd" provides all type of tutorials site rookienerd Our one and only site provides all type of tutorials

String Comparison

Perl String Comparison Example, eq

Perl strings are always compared with eq rather than (==). It checks whether two strings are equal or not.

Example
snippet
my $string1 = "Ana";
my $string2 = "Ana";
if($string1 eq $string2) {
    print "Match!\n";
}
my $string3 = "Ana";
my $string4 = "Christian";
if($string3 eq $string4) {
    print "Match\n!";
}else{
    print "Missmatch!\n";
}
Output
Match! Missmatch!

Determine String Length, length()

Perl string length can be determined with length() function.

Example
snippet
my $msg = "Our site rookienerd provides all type of tutorials";
print "String Length : ", length($msg), "\n";
Output
String Length : 50

Replace a string with another string, s///g

A string can be replaced with another string in two ways.

In first one, we have replaced Tigers with Lions which occurs single time in the string with s///.

In second one, we have replaced roses with flowers globally with s///g.

Example
snippet
my $var1 = "Tigers are big and frightening.";
$var1 =~ s/Tigers/Lions/;
print "$var1\n";
my $var2 = "Red roses are very popular. Yellow roses are less seen.";
$var2 =~ s/roses/flowers/g;
print "$var2\n";
Output
Lions are big and frightening. Red flowers are very popular. Yellow flowers are less seen.

Finding a match in the string, =~

Perl provides a match operator (=~) to find a substring from a string.

Example
snippet
my $var = "Tigers are big and frightening.";
if($var =~ /frightening/)
{
	print "Matched\n";
}else{
	print "Match not Found\n";
}
if($var =~ /biggest/)
{
	print "Matched\n";
}else{
	print "Match not Found\n";
}
Output
Matched Match not Found

Concatenating two Strings (.=)

Two strings can be joined together using (.=) operator.

Example
snippet
my $str1 = "Where there is a will,";
my $str2 = "there is a way.\n";
my $joining = '';
$joining = $str1 . ' ';
$joining .= $str2;
print $joining;
Output
Where there is a will, there is a way.
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +