Perl String Functions: Basics for String Manipulation

perlstringfunctionsFunctions are a sequence of statements that perform specific operations. Perl contains many predefined functions that are useful in information manipulations. In Perl, you use a function as an expression. As soon as Perl sees a function call in the script, the function line is executed. Perl functions can be grouped as the following:

  • String functions
  • Numeric functions
  • Array functions
  • Time functions
  • Pattern Matching Functions

Learn Perl from scratch using a tutorial at Udemy.com

String functions: Strings are a combination of characters. The string has no limit and it can be of any size and may contain any characters, symbols or words. In Perl, it is defined by placing them in double quotes or with the ‘q’ functions.

Example

$hello = “Hello “I am String;

$user = q^I am also String^;

Learn more about Perl at Udemy.com

String Functions in Perl:

chomp: Removes the trailing characters from a string or from string arrays. It only removes whitespaces at the end of the string passed to it. It returns the count of characters removed.

Syntax:

chompvariable_name

chomp (LIST)

chomp

Example:

$string1 = "I am String";

$retval  = chomp( $string1 );// Chomps the last whitespace

print " Chomp String is : $string1\n";

print "Characters Removed : $retval\n";

$string1 = "I am String  \n";

$retval  = chomp( $string1 ); // Chomps the last whitespace

print " Chomp String is : $string1\n";

print "Characters Removed : $retval\n";

Output:

Chomp String is : I am String

Characters Removed : 0

Chomp String is : I am String

Characters Removed : 1

 

chop: This  function removes the last character from strings or strain arrays. The function returns the character removed.

Syntax:

chopVariable_name

chop( LIST )

chop

Example:

 
$string1 = "I am String";

$retval  = chop( $string1 ); // Chops the last character of the String

print "Chop String is : $string1\n";

print "Characters Removed : $retval\n";

Output

Chop String is : I am String

Characters Removed : g

cur: This function converts ASCII or Unicode values to its equivalent characters. It returns the converted character.

Syntax:

chr EXPR

Example :

print chr (35);// Will convert the ASCII value to character

print "\n";

print chr (38);

print "\n";

print chr (40);

print "\n";

print chr(0x263a);

Output:

#

&

(

 

lcfirst,ucfirst, lc,uc, length:

  • The “lcfirst” is used to convert first character to lowercase.
  • “ucfirst” is used to convert first character into uppercase.
  • The “lc” is used convert the string in to lowercase.
  • The “uc” is used to convert the string into uppercase
  • The “length” function returns the number of character in the string.

Learn more about Perl at Udemy.com.

Syntax:

lcfirst EXPR

lcfirst

ucfirst EXPR

ucfirst

length EXPR

length

lc EXPR

lc

uc EXPR

uc

Example:

my $str = 'HeLlo';

print lc $str; //Returns the lowercase

print "\n" ;

print uc $str;  // Returns the uppercase

print "\n";

print length $str; // Returns length of the string

print "\n";

print lcfirst $str;  // Returns first letter lowercase in string

print "\n";

print ucfirst $str;  // Returns first letter uppercase in string

print "\n";

Output:

hello

HELLO

5

heLlo

HeLlo

 

index: This function takes two string as arguments and return the position of occurrence of second string in the first. If the position index is given, the search starts from that index. If the string does not match then -1 is returned. It is a case sensitive match of characters.

Syntax:

index STR, SUBSTR, POSITION

index STR, SUBSTR
  • STR: Is the string to search in.
  • SUBSTR: It is the character or substring, which we want to look for.
  • POSITION: The start index of the search from where the substring searching would begin.

Example:

$string = "Jack and Jill went up to the hill";

$index = index ($string, 'went'); // Returns the index of went

print "String found at $index\n";

$index = index ($string, 'went',15);// Will search for "went" from starting index 15

print "String found at $index\n";

$index = index ($string, 'Went');

print "String found at $index\n";

Output:

String found at 14

String found at -1

String found at -1

 

rindex: It is the reverse of the index functions and returns the last occurrence of the character found i.e. it searches the string for the character or substring from right to left.

Syntax:

rindex STR,SUBSTR,POSITION

rindex STR,SUBSTR

Example:

$string = "Jack and Jill went up to the hill";

$index = rindex ($string, 'went');// will search for the substring "went"

print "String found at $index\n";

$index = rindex ($string, 'to'); //will search for the substring "to"

print "String found at $index\n";

String found at 14

String found at 22

Example 2: Finding all the substring

my $abc = "Jack Jill went to hill Jack falls and Jacks smiles";

my $pos = length($abc)-1;//Calculating length of the string

print "Jack found at positions: ";

while(1)

{

$pos = rindex($abc, "Jack", $pos);

last if($pos< 0);

print $pos--, " ";

}

 

print "\n";

Output:

Jack found at positions: 38 23 0

 

substr: The most important string function. It is just opposite of the index function while the index gives the position of the substring this function retrieves and returns the substring. It is basically used for extraction of substring from predefined index locations.

Syntax:

substr STR, OFFSET, LENGTH, REPLACEMENT

substr STR, OFFSET, LENGTH

substr STR, OFFSET

  • STR: It is the string from which substring will be extracted.
  • OFFSET: It is starting index from where the extraction will begin
  • LENGTH: It provides the length of the string to extract.
  • REPLACEMENT: It is string that replaces the extracted substring.

Example:

$temp = substr("Jack and Jill", 4);// Will return the complete String after index number 4

print "Substring value is $temp\n";

$temp = substr("Jack and Jill", 5,3);// Returns the substring of length 3 starting from 5

print "Substring value is $temp\n";

$temp = substr("Jack and Jill", 5,Jack);

Output:

Substring value is  and Jill

Substring value is and

 

Example 2:

$names = "Jack and Jill are enemies";

print "$names\n";

substr($names, 18, 7) = "friends";//"Friends" will be replaced  from index 18 replacing "enemies" in the string

print "$names\n";

Output:

Jack and Jill are enemies

Jack and Jill are friends