Perl chomp()和chomp()
Perl chop()和chomp()
两者的功能非常相似。它们都从给定字符串的末尾删除一个字符。
Perlchop()
Perlchop() 函数从字符串中删除最后一个字符,不管那个字符是什么。它从字符串中返回切碎的字符。
语法:
Perlchop() 示例
#chop() EXAMPLES
$a = "AEIOU";
chop($a);
print"$a\n"; #it will return AEIO.
$a = "AEIOU";
$b = chop($a);
print"$b\n"; #it will return U.
输出:
看输出,首先是斩波后打印变量$a。然后变量 $b 被打印出来,它从字符串中返回切碎的字符。
Perl chomp()
chomp() 函数从字符串中删除任何换行符字符串的结尾。它返回从字符串中删除的字符数。
语法:
Perl chomp() 示例
#chomp() EXAMPLES
$a = "AEIOU";
chomp($a);
print"$a\n"; #it will return AEIOU.
$a = "AEIOU";
$b = chomp($a);
print"$b\n"; #it will return 0, number .
$a = "AEIOU\n";
chomp($a);
print"$a\n"; #it will return AEIOU, removing new line character.
$a = "AEIOU\n";
$b = chomp($a);
print"$b\n"; #it will return 1, number of characters removed.
输出:
看输出,首先,变量$a不包含任何新的单独字符。然后它被传入变量 $b 并打印。由于没有删除任何字符,它返回 0。
现在,变量 $a 包含一个换行符。当它传入 $b 时,它返回 1,因为它删除了一个新行字符。