Updated April 20, 2023
Introduction to Perl substr
In Perl, substr is a function for finding a part of the string in the given or specified string which requires the index of each character to find the substring of the string in which the length of the string is required for accessing the substring from the given string. In general, substr() is a string function in Perl for getting the part of the given or specified string, where this function returns the substring from the specified length as the argument to this function and if the length is not specified then by default it will print the string from the starting of the given string.
Working of substr() in Perl
In this article, we will discuss substr() function of the Perl programming language. In Perl, substr() is a string function which means applied basically on strings where this function takes in the string as an argument of which the substring is needed along with a length of the string such as the index of the string from one character to another character is also passed as an argument but if not specified it will by default print the string by the starting of the index of the given string which remains provided the index of the string can be in positive or negative numerical which indicates the direction of the index count in the string where negative index reads the string from right to left and positive index specification reads the index from left to right of the string. This function can also be used for replacing one string with another string by passing the string that must be replaced with some other part of the string to be replaced is also passed to this function and only that part of the string is replaced.
Now let us see syntax and examples in the below section:
Syntax for substr() in Perl:
substr(string, indx_str, len_str);
Syntax for substr() used for replacing the string is as follows:
substr(string, indx_str, len_str, str_replace);
In the above syntaxes the parameters are:
- string: this parameter is used for specifying the string from which the substring is to be removed and displayed.
- indx_str: this is used for specifying the index of the given string from starting either in positive or negative.
- len_str: this is used for specifying the length of the substring that needs to be extracted from the specified string. This parameter can also be optional.
- str_replace: this is used only when we want to replace any part of the given string with another specified substring. So this parameter is optional.
Examples
In the below example we will see simple Perl code for demonstrating substr() function.
Example #1
Code:
#!/usr/bin/perl
print "Demonstration of substr() function in Perl \n";
print "\n";
$str1 = "Educba";
print "The given string is: ";
print $str1;
print "\n";
$res_substr = substr($str1, 3);
print "The substring obtained from the given string is :";
print $res_substr;
print "\n";
$res_substr2 = substr($str1, 2, 4 );
print "The substring using len_str parameter in substr(): ";
print $res_substr2;
print "\n";
$res_substr3 = substr($str1, 1, -2);
print "The substring using negative index in substr() function: ";
print $res_substr3;
Output:
In the above program, we can see first we have declared a string with variable “str1” starting with the “$” symbol and the string is “Educba”. Firstly, we have printed the defined string and then we are using substr() function where we are passing only two parameters first parameter is the string that was defined at the starting( str1 = “Educba”), and the second parameter we have passed as “3” which is an index of the given string from where it will print all the characters or substring from index 3 to the remaining of the characters in the string and the result of substring is stored in “$res_substr”. Then we have again used substr() function in which we have passed three parameters where the first parameter is the specified string, second as “2” which is an index of the given string so at the index 2 we have “u” from the character “u” of the given string to the third parameter specified as 4 which is the length of which how many after index “2” must be in the substring so from index 2 it will print 4 characters next of the specified string as a substring and this result is stored in “$res_substr2”. Then lastly we have specified negative values in the third parameter which will start the indexing from right to left of the string so here at the index “-2” we have “b” but we have to take the behind the character that is after the character at -2 from right to left which will be “c” so it will print from index “1” as “d” to index -2 as “c” and the result is stored in “$res_substr3”. The output is as shown in the above screenshot.
Now we will see an example for string replacement using substr() function in Perl:
Example #2
Code:
#!/usr/bin/perl
print "Demonstration of substr() function used in replacement of string in Perl \n";
print "\n";
$str = "Educba Institute for Java";
print "The given string is: ";
print $str;
print "\n";
$res = substr($str, 21, 4, "Python");
print "The string that needs to be replaced is: ";
print $res;
print "\n";
print "After replacement of the string the new string is: ";
print $str;
Output:
In the above program, we declared a string as “Educba Institute for Java” which is printed at the first then we are applying substr() function where we are passing four parameters where first is the string specified, second as 21 which is the index from where the string needs to be replaced, third as 4 which is the length of the string that needs to be replaced and fourth is the substring that needs to be printed after replacing the earlier substring. The result is stored in “$res” and the output can be seen in the above screenshot.
Conclusion
In this article, we conclude that the substr() function in Perl is a string function where it extracts the part of the string from the given string. In general, this function returns the substring from the given string using the string index and length for the substring. In this article, we saw an example of displaying the substring using the string index and length of the string which can have both positive and negative indexing with an example. In this article, we also saw how substr() function can be used in the replacement of the strings with an example.
Recommended Articles
This is a guide to Perl substr. Here we also discuss the Definition and Working of Perl substr in Perl along with different examples and its code implementation. You may also have a look at the following articles to learn more –