Perl chomp函数


PPS:学习Perl语言编程中……,打算边学边把Perl函数整理出来。

介绍:

chomp函数通常会删除变量里包含的字符串尾部的换行符。它是chop函数的一个略微安全些的版本,因为它对没有换行符的字符串没有影响。更准确地说,它根据了解$/的当前值删除字符串终止符,而不只是最后一个字符。

和chop不同,chomp返回删除的字符数量。你不能chomp一个直接量,只能处理变量。

用法:

chomp VARIABLE

chomp LIST

chomp

例子:

#!/usr/bin/perl

$string1 = "This is test";
$retval  = chomp( $string1 );

print " Choped String is : $string1\n";
print " Number of characters removed : $retval\n";

$string1 = "This is test\n";
$retval  = chomp( $string1 );

print " Choped String is : $string1\n";
print " Number of characters removed : $retval\n";

结果如下:

 Choped String is : This is test
 Number of characters removed : 0
 Choped String is : This is test
 Number of characters removed : 1

chomp函数 完~


《“Perl chomp函数”》 有 5 条评论