功能 substr() Perl

在Perl编程语言都有一个内置的功能 substr(). 你可以使用这一功能工作时用弦。 使用这一功能,可以获得的一部分一字符串,或者替换一部分一字符串的另一串。

这里的一个例子是接收一部分的一串:

▶ Run
#!/usr/bin/perl

my $str = 'Hello, world!';

print "'" . substr($str, 7, 5) . "'";

这个程序将显示该文本 'world'. 呼叫功能 substr($str, 7, 5)说 你需要从行 $str 五个字,开始从七位置。 在此使用 值 $str 不会改变。

当使用 substr() 有四个参数的变化价值的一个变量。 这里是一个例子使用 substr() 以更换:

▶ Run
#!/usr/bin/perl

my $str = 'Hello, world!';

print "'" . substr($str, 7, 5, 'you') . "'\n";
print $str;

输出的程序:

'world'
Hello, you!

返回值当你呼叫 substr($str, 7, 5, 'you') 是完全一样,当你打电话 substr($str, 7, 5), 但有四个论点以及甚至改变价值的变量。 这个词 world 改为 you. 请 注意到替换是正确的,尽管事实是在原来的文字和新词不同的号码 字母。

参数

my $str1 = substr($str, $offset);
my $str2 = substr($str, $offset, $length);
my $str3 = substr($str, $offset, $replacement);

功能 substr() ,则必须指定两个、三个或四个论点。

在这种情况下,如果你尝试使用功能 substr() 没有参数,或与一个 参数,它将错误:

Not enough arguments for substr at script.pl line 3, near "substr()"
Execution of script.pl aborted due to compilation errors.

当使用两个论点 substr($str, $offset) 的功能将返回 所有的字符串 $str 开始在位置 $offset ,以结束该行。

当使用的三个参数 substr($str, $offset, $length) 的职能返回 $length 字符串 $str 起与所在位置 $offset.

当使用 substr() 带两个或三个参数,原始string value 不会改变。

如果您使用的四个论点 substr($str, $offset, $replacement)功能 返回的相同情况的三个参数,但另外 代替原来的字符串中,值得 $replacement.

回值

功能 substr() 可能返回 undef,或一串。

undef 返回,如果你试图让元从字符串 指数大于长串。 这里是一样的程序,返回 undef:

▶ Run
#!/usr/bin/perl

use Data::Dumper;

my $str = 'Hello, world!';
my $str1 = substr($str, 10000);

warn Dumper $str1;

在事件,如果在程序码将 use warnings;,该程序仍然会 此外,把在警告屏幕:

substr outside of string at script.pl line 8.

负值 $offset

功能 substr() 允许指定负值,对于第二个论点。 例的程序:

▶ Run
#!/usr/bin/perl

my $str = 'Hello, world!';
print substr($str, -3);

屏幕上显示的案文 ld! 是最后三个字符串 Hello, world!.

一个负面的第二个论点表明,位置是计算从端行。

substr($str, -3) 是相当类似的 substr($str, -3, 3).

负值 $length

值的第三个论点可以是负面的。 这里是例:

▶ Run
#!/usr/bin/perl

use feature qw(say);

my $str = 'Hello, world!';

say substr($str, 7, -1); # world
say substr($str, 7, -2); # worl
say substr($str, 7, -3); # wor

官方文件

这里是输出的命令 perldoc -f substr:

    substr EXPR,OFFSET,LENGTH,REPLACEMENT
    substr EXPR,OFFSET,LENGTH
    substr EXPR,OFFSET
            Extracts a substring out of EXPR and returns it. First character
            is at offset zero. If OFFSET is negative, starts that far back
            from the end of the string. If LENGTH is omitted, returns
            everything through the end of the string. If LENGTH is negative,
            leaves that many characters off the end of the string.

                my $s = "The black cat climbed the green tree";
                my $color  = substr $s, 4, 5;      # black
                my $middle = substr $s, 4, -11;    # black cat climbed the
                my $end    = substr $s, 14;        # climbed the green tree
                my $tail   = substr $s, -4;        # tree
                my $z      = substr $s, -4, 2;     # tr

            You can use the "substr" function as an lvalue, in which case
            EXPR must itself be an lvalue. If you assign something shorter
            than LENGTH, the string will shrink, and if you assign something
            longer than LENGTH, the string will grow to accommodate it. To
            keep the string the same length, you may need to pad or chop
            your value using "sprintf".

            If OFFSET and LENGTH specify a substring that is partly outside
            the string, only the part within the string is returned. If the
            substring is beyond either end of the string, "substr" returns
            the undefined value and produces a warning. When used as an
            lvalue, specifying a substring that is entirely outside the
            string raises an exception. Here's an example showing the
            behavior for boundary cases:

                my $name = 'fred';
                substr($name, 4) = 'dy';         # $name is now 'freddy'
                my $null = substr $name, 6, 2;   # returns "" (no warning)
                my $oops = substr $name, 7;      # returns undef, with warning
                substr($name, 7) = 'gap';        # raises an exception

            An alternative to using "substr" as an lvalue is to specify the
            replacement string as the 4th argument. This allows you to
            replace parts of the EXPR and return what was there before in
            one operation, just as you can with "splice".

                my $s = "The black cat climbed the green tree";
                my $z = substr $s, 14, 7, "jumped from";    # climbed
                # $s is now "The black cat jumped from the green tree"

            Note that the lvalue returned by the three-argument version of
            "substr" acts as a 'magic bullet'; each time it is assigned to,
            it remembers which part of the original string is being
            modified; for example:

                my $x = '1234';
                for (substr($x,1,2)) {
                    $_ = 'a';   print $x,"\n";    # prints 1a4
                    $_ = 'xyz'; print $x,"\n";    # prints 1xyz4
                    $x = '56789';
                    $_ = 'pq';  print $x,"\n";    # prints 5pq9
                }

            With negative offsets, it remembers its position from the end of
            the string when the target string is modified:

                my $x = '1234';
                for (substr($x, -3, 2)) {
                    $_ = 'a';   print $x,"\n";    # prints 1a4, as above
                    $x = 'abcdefg';
                    print $_,"\n";                # prints f
                }

            Prior to Perl version 5.10, the result of using an lvalue
            multiple times was unspecified. Prior to 5.16, the result with
            negative offsets was unspecified.

相关的主题

其他文章