Perl String

String is a combination of sequence of single and multiple character which are make a words. String text can be form of UNICODE characters, Escape sequences and symbols . Which has been surrounded of single and double quotes.

#!/usr/bin/perl
use warnings;
use strict;
use utf8; #for unicode char

my $text = "ABCDEF";
my $unicode ="∰ : ≸ : ≹ : ╳";
my $escape = "A\nB\tC";
#display text value
print $text,"\n";
print $unicode,"\n";
print $escape,"\n";
ABCDEF
Wide character in print at text.pl line 11.
∰ : ≸ : ≹ : ╳
A
B	C

Double quotes are used to interpolate of string variable.

#!/usr/bin/perl
use strict;
use warnings;

my $name = "Foo";

print "Name : $name","\n";
print 'Name : $name',"\n";
Name : Foo
Name : $name

Perl Escape Sequences

Escape Sequences Name Overview
\b Backspace Move the course one character to the left
\n Newline Shifting cursor into new line
\' Single Quotation Mark Printing a quotation mark
\" Double Quotation Mark Printing double quotation mark
\t Horizontal Tab Move the cursor on next tab position
\\ Backslash Print one backslash
\r Carriage Return Move the cursor to beginning of current line
#!/usr/bin/perl
use warnings;
use strict;

print "A\n";

print "\tB\n";

print "C\bD\n";

print "E\"F\n";

print "G\'H\n";

print "I\fJ\fK\n";

print "L\\ \n";

print "M N O \r P Q \n";
A
	B
D
E"F
G'H
I
 J
  K
L\ 
 P Q 

Perl String Methods

Methods Overview
chr Returns the unicode character of given number
index Returns the first occurrence index of substring which is found in given text
lc Returns the lowercase characters (small case character) of given text string
length Returns the number of characters of given string
oct Converting an octal number into decimal value
ord Returns the ASCII value of the first character of a string
reverse Returns the Reverse order of given text string
rindex Returns the last occurrence index of substring which is found in given text
sprintf Returns the formatting of scalar string
substr Returns substring or modifies a substring in a string
uc Returns the uppercase characters (capital letter) of given text string

chr

#!/usr/bin/perl
use warnings;
use strict;
use open ':std', ':encoding(UTF-8)';
#chr are display unicode character of given number 
print chr(97) ,"\n"; #a
print chr(65) ,"\n"; #A
print chr(225) ,"\n"; #á
a
A
á

index

#!/usr/bin/perl
use warnings;
use strict;

print (index "Runing Test Case","Test"); # 7
print ("\n");

#When match given text are not exist in string
print (index "Runing Test Code","test"); # -1
print ("\n");

#When given occurrence are exist more than once
print (index "Programming Test","m"); # 6
print ("\n");
7
-1
6

lc

#!/usr/bin/perl
use warnings;
use strict;
#Returning lowercase characters of given text
print (lc "PrograMmer");
print ("\n");

print lc("ruN Code");
print ("\n");

print lc("TEST CODE");  
print ("\n");
programmer
run code
test code

length

#!/usr/bin/perl
use warnings;
use strict;

print (length "A..Z");
print ("\n");

#count white characters
print (length "A - | - Z");
print ("\n");

print (length "Regularcodes");  
print ("\n");
4
9
12

oct

#!/usr/bin/perl
use warnings;
use strict;

print (oct '33'); # 27
print ("\n");


print (oct '14'); # 12
print ("\n");


print (oct '21'); # 17
print ("\n");

ord

#!/usr/bin/perl
use warnings;
use strict;
use utf8;

my $om = 'ॐ';
my $num = ord($om);

print  "Uincode $om : ",ord($om),"\n";

print  pack("U", $num),"\n"; #number to unicode
Wide character in print at str.pl line 9.
Uincode ॐ : 2384
Wide character in print at str.pl line 11.
ॐ

reverse

#!/usr/bin/perl
use warnings;
use strict;
#reverse string text 
print scalar (reverse "A..Z");
print ("\n");

print scalar reverse("A - | - Z");
print ("\n");

print scalar reverse("Codes");  
print ("\n");
Z..A
Z - | - A
sedoC

rindex

#!/usr/bin/perl
use warnings;
use strict;

print (rindex "Run X-Ray Test to X-Men","X"); # 18
print ("\n");

#When match given text are not exist in string
print (rindex "Runing Test Code","test"); # -1
print ("\n");

print (rindex "Programming Test","m"); # 7
print ("\n");
18
-1
7

sprintf

#!/usr/bin/perl
use warnings;
use strict;
print sprintf("%07d", '101'),"\n"; 
print sprintf("%7d", '101'),"\n"; 
0000101
    101

substr

substr() is an useful method which are used to extract the substring of given text.

substr(string, start_index, length)

Length can be form of positive and negative numbers, when getting the fixed length substring. And length is an optional value.

#!/usr/bin/perl
use warnings;
use strict;

my $text = "Learn Programmings";

print substr($text,1),"\n";

print substr($text,6),"\n";

print substr($text,6,3),"\n";

print substr($text,6,-5),"\n";
earn Programmings
Programmings
Pro
Program

uc

#!/usr/bin/perl
use warnings;
use strict;
#Returning uppercase of given text
print (uc "Programmer");
print ("\n");

print uc("run code");
print ("\n");

print uc("tesT Code ");  
print ("\n");
PROGRAMMER
RUN CODE
TEST CODE


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved