Perl Substr() function and its application and syntax

Perl Substr() function and its application

Perl substr() function example, substr() function example code - Returns a substring of EXPR, starting at offset OFFSET within the string.

grammar:

subVYRoBPstr EXPR, OFFSET, LEN, REPLACEMENT

substr EXPR, OFFSET, LEN

substr EXPR, OFFSET

Definition and usage

Returns a substring of EXPR, starting at offset OFFSET within the string. If OFFSET is negative, start the string with that many characters ending. If LEN is specified, returns the number of bytes, or all bytes until the end of the string (if not specified). If len is negative, how many characters to end the string.

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

If you specify a substring, past the end of the string, only valid elements of the original string are returned.

return value

string

example

Try the following example:

#!/usr/bin/perl -w

#by yiibai.com

$temp = substr("okay", );

print "Substring valuye is $temp\n";

$temp = substr("okay", ,);

print "Substring valuye is $temp\n";

This will produce the following result:

Substring valuye is ay

Substring valuye is ka

Instance analysis of Perl substr function application

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

Application of Perl substr function

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

$str="testtest";

printsubstr($str,0,5);

$str="testtest";

printsubstr($str,0,5);

Run the above program, the result is "testt", this is the result we want. Look at the following program again:

$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 treated as bytes, "printsubstr($str,0,1);" This sentence just puts "test text" The first byte is taken out and output with print, but a single byte cannot represent a Chinese character, so "?" is output.

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, let perl treat the "test text" as a string, and then use "substr($str, 0,1);"The interception is not a byte, but a Chinese character.

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

Command: substr

Syntax: substr($string, offset, length)

offset represents the position of the starting character, length represents the length of the quoted string, and if length is omitted, it represents the length from the start value to the last character of the string. and

If offset is a negative value, characters will be 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";

Guess you like

Origin blog.csdn.net/G171104/article/details/131902269