SoFunction
Updated on 2025-04-07

Perl Substr function and application of functions

perl substr() function example, substr() function example code - Returns the substring of EXPR, starting with OFFSET in the string.

grammar:

substr EXPR, OFFSET, LEN, REPLACEMENT
substr EXPR, OFFSET, LEN
substr EXPR, OFFSET

Definition and usage

Returns the substring of EXPR, starting with the offset within the string OFFSET. If OFFSET is negative, there are many characters ending strings starting with. If LEN specifies, the number of bytes returned, or all bytes until the end of the string (if not specified). If len is negative, how many characters of the string end?

If you replace the substring specified by REPLACEMENT, replace the string of REPLACEMENT.

If you specify a substring, crosses the ending string, return the only valid element of the original string.

Return value

String

example

Try the following example:

#!/usr/bin/perl -w
#by 
$temp = substr("okay", );
print "Substring valuye is $temp\n";
$temp = substr("okay", ,);
print "Substring valuye is $temp\n";

This will produce the following results:

Substring valuye is ay
Substring valuye is ka

Example analysis Perl substr function application

This article will focus on the application of Perl substr function. When writing a perl program, we sometimes need to intercept part of the content in a string. At this time, we usually use the substr function to implement this function.

Perl substr function application

When writing a perl program, we sometimes need to intercept part of the content in a string. At this time, we usually use the Perl substr function to implement this function.

$str="testtest"; 
printsubstr($str,0,5); 
$str="testtest"; 
printsubstr($str,0,5); 

Run the above program and output the result "testt", which is the result we want. Let’s take a look at the following program:

$str="Test text"; 
printsubstr($str,0,1); 
$str="Test text"; 
printsubstr($str,0,1); 

At this time, a "?" is output, which is obviously not the result we want. Because in perl, all strings input from the outside (including strings written in the program) will be processed as bytes, "printsubstr($str,0,1);" is just to take out the first byte of the "test text" and output it with print. However, a single byte cannot represent a Chinese character, so it outputs "?".

If you want the above program to output the correct result, you need to use the decode function to convert the "test text" into a perl internal string, and let perl process the "test text" as a string. In this way, the intercepting of "substr($str,0,1);" is not a byte, but a Chinese character.

Let’s take a look at the usage of Perl substr function:

Directive: substr

Syntax: substr($string,offset,length)

offset represents the position of the starting character, length represents the length of the referenced string. If length is omitted, it represents the length of the character from the starting value to the last character of the string. and

If offset is negative, the character is specified from the right side of the string.

Example:

$s=substr("perl5",2,2);#At this time $s="rl";$s=substr("perl5",2);#At this time $s="rl5";$s=substr("perl5",-2,2);#At this time$s="er";