Perl

Use of PERL substr() Function

“PERL substr() function is mainly used to cut the particular portion of the main string. This function can also be used for replacing the portion of the main string with another string. Different ways to cut or cut and replace a particular portion of the string have been shown in this tutorial by using the PERL script.”

Syntax
The syntax of the substr() function is mentioned below.

substr (mainString, startIndex, [,length [, replaceString]])

  • mainString: The first argument is used to store the main string from which the substring will be cut.
  • startIndex: The second argument is used to set the starting position for cutting or replacing the substring. The value of this argument can be positive or negative.
  • length: The third argument is used to set the length of the substring. The value of this argument can be positive or negative, and this argument is optional.
  • replaceString: The fourth argument is used to replace the particular portion of the main string based on the values of the second and the third arguments. This argument is optional.
  • The substring of the particular length is returned by the substr() function.

Different uses of the substr() function have been shown in the next part of this tutorial.

Example-1: Use of substr() Function Without Optional Argument

Create a PERL file with the following code that shows the use of the substr() function without any optional argument. The positive starting index value has been used in the first substr() function. So, the index value will start counting from the 0 value and the left side. The negative starting index value has been used in the second substr() function. So, the index value will start counting from 1 value and the right side. The output of both substr() functions has been printed later.

#!/usr/bin/perl
#Define the string value
$string = "Welcome to Linuxhint";
#Print the orginal value
print "The original string is:\n", $string, "\n";
#Cut the substring using positive index value
$substring = substr($string, 11);
print "The substring value by using positive index:\n", $substring, "\n";
#Cut the substring using negative index value
$substring = substr($string, -9);
print "The substring value by using negative index:\n", $substring, "\n";

Output:
The following output will appear after executing the above code.

Example-2: Use of substr() Function With the First Optional Argument

Create a PERL file with the following code that shows the use of the substr() function with the first optional argument. The positive starting index value has been used in both substr() functions here. The positive length value has been used in the first substr() function. So, the length value will be counted from the left. The negative length value has been used in the second substr() function. So, the length value will be counted from the right. The length value starts counting from 1 for both positive and negative values. The output of both substr() functions has been printed later.

#!/usr/bin/perl
#Define the string value
$string = "Welcome to Linuxhint";
#Print the orginal value
print "The original string is:\n", $string, "\n";
#Cut the substring using positive index and length values
$substring = substr($string, 3, 4);
print "The substring value by using positive index:\n", $substring, "\n";
#Cut the substring using positive index and negative length values
$substring = substr($string, 11, -4);
print "The substring value by using negative index:\n", $substring, "\n";

Output:
The following output will appear after executing the above code.

Example-3: Use of substr() Function With All Optional Arguments

Create a PERL file with the following code that shows the use of the substr() function with all optional arguments. The substr() function has been used three times here. In the first substr() function, the matching substring will be replaced by the string, “PHP.” In the second substr() function, the matching substring will be replaced by the string, “Script.” In the third substr() function, the matching substring will be replaced by the empty string. The output of all substr() functions has been printed later.

#!/usr/bin/perl
#Define the string value
$string = "Learn PERL programming";
#Print the orginal value
print "The original string is:\n", $string, "\n";
#Replace the substring using positive index and length value
$replace1 = substr($string, 6, 4, "PHP");
print "The replaced string by using positive index and length:\n", $string, "\n";
#Replace the substring using negative index and positive length value
$replace1 = substr($string, -11, 11, "Script");
print "The replaced string by using negative index and positive length:\n", $string, "\n";
#Replace the substring using positive index and negative length value
$replace1 = substr($string, 0, -10, "");
print "The replaced string by using positive index and negative length:\n", $string, "\n";

Output:
The following output will appear after executing the above code.

Example-4: Extract Value From a String Using substr() Function

Create a PERL file with the following code that will cut the date value and the time value separately from a string. Here, the $datetime variable has been used to store a string value that contains a particular date and time value. The first substr() function has been used to retrieve the date value, and the second substr() function has been used to retrieve the time value from the $datetime variable. The output of all substr() functions has been printed later.

#!/usr/bin/perl
#Define the string value
$datetime = "17-06-22 05:30 AM";
#Print the orginal value
print "The original string is:\n", $datetime, "\n";
#Cut the date value from the string
$date = substr($datetime, 0, 8);
#Print the date value
print "The date value is:\n", $date, "\n";
#Cut the time value from the string
$time = substr($datetime, 9);
#Print the time value
print "The time value is:\n", $time, "\n";

Output:
The following output will appear after executing the above code.

Conclusion

The uses of the substr() function with different types of argument values have been shown in this tutorial by using multiple examples. I hope this tutorial will help the PERL users to know the purpose of using the substr() function in PERL.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.