logo codesdope

String in Perl


You have already been introduced with strings. String is a collection of characters and these are written within (' ') or (" "). Like array, the position of its characters also starts from 0. It means that in the string "Hello", "H" is at position 0. Strings written between (' ') are non-interpolated strings. Thus special variables like $, @, \n, etc will not be interpolated when written inside single quotes (' '), whereas strings written inside (" ") are interpolated strings. Let's see an example:

print 'Hello World\n';
print "\n";
print "Hello World!\n";
Output
Hello World\n
Hello World!

You can see that \n written inside single quote (' ') didn't get interpolated and got printed as \n. To print an email address, we always use single quote. Let's see an example:

print 'abc@example.com';
print "\n";
print "abc@example.com\n";
Output
abc@example.com
abc.com

qq and q operators


qq (double-q operator) and q(single-q operator) can be used instead of double quotes(" ") and single quotes(' ') respectively. Let's see its example:

print q{(abc@example.com)};
print "\n";
print qq{(abc@example.com)\n};
Output
(abc@example.com)
(abc.com)

x operator


I have already told you about the x operator but I am mentioning it here again. It is used to multiply a string. Let's see its example:

print "abc"x3,"\n";
Output
abcabcabc
Perl x operator

Some useful functions


length


length is used to find the length or size of a string. Let's see it working:

$a = "abcdefghij";
print length($a),"\n";
Output
10

substr


substr is used to extract a substring from a string. It means that it is used to extract a part of a string. Mostly, substr is used in these forms:

  • substr(string,OFFSET)
  • - It returns a substring from OFFSET+1th character ( the first character is at offset 0 ) to the end of the string. E.g. - substr("abcd",1) will give "bcd".
  • substr(string,OFFSET,LENGTH)
  • - It returns a substring from the character at offset OFFSET to the character at offset LENGTH-1. E.g.- substr("Hello there",1,5) will give "ello".
Let's see an example of this:

$a = "Hey there World";
print substr($a,4),"\n";
print substr($a,0,3),"\n";
Output
there World
Hey

chr


chr(NUMBER) returns the character for the NUMBER in the ASCII chart. For example, chr(70) will return F.

print chr(70),"\n";
Output
there World
Hey

index and rindex


index(STRING,SUBSTRING,POSITION) and rindex(STRING,SUBSTRING,POSITION) returns the index of the first and the last occurrence of SUBSTRING in STRING at or after POSITION respectively. If we don't specify POSITION, the search starts at the beginning of STRING.

print index("Hey there!Hey","y"),"\n";
print rindex("Hey there!Hey","y"),"\n";
Output
2
12

join


join(STRING,ARRAY) is used to join and return a string after joining every element of ARRAY by STRING. For example, join("-",(1,2,3)) will return "1-2-3".

print join("-",(1,2,3)),"\n";
Output
1-2-3

lc


lc(STRING) returns a string with every character of STRING in lowercase.

print lc("Hey EveryONE"),"\n";
Output
hey everyone

lcfirst


lcfirst(STRING) returns a string with only the first character of STRING in lowercase.

print lcfirst("Hey EveryONE"),"\n";
Output
hey EveryONE

uc and ucfirst


Same as lc and lcfirst but transforms to uppercase.

print ucfirst("hey EveryONE"),"\n";
print uc("hey EveryONE"),"\n";
Output
Hey EveryONE
HEY EVERYONE

split


split is reverse of join. It is used to split the elements of a string and store them into an array. We use split as:

  • split /PATTERN/,STRING,LIMIT
  • - This will split the STRING whenever PATTERN will match but will stop if the number of elements becomes equal to LIMIT. It means that it will return the number of elements less than or equal to LIMIT.
  • split /PATTERN/,STRING
  • - Same as above but no restriction of LIMIT.
  • split /PATTERN/
  • - If no string is given, then Perl will split $_ (a special variable).
  • split
  • - splits $_ for /\s+/.
/PATTERN/ in the split function is a regular expression which you will learn in the next chapter.
It will be clearer from the example given below:

@a = split(/-/, "1-2-3");
print "@a\n";
Output
1 2 3

Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.