珀尔 | chomp()函数
Perl 中的 chomp()函数用于从输入字符串中删除最后一个尾随换行符。
Syntax: chomp(String)
Parameters:
String : Input String whose trailing newline is to be removed
Returns: the total number of trailing newlines removed from all its arguments
示例 1:
#!/usr/bin/perl
# Initialising a string
$string = "GfG is a computer science portal\n";
# Calling the chomp() function
$A = chomp($string);
# Printing the chopped string and
# removed trailing newline character
print "Chopped String is : $string\n";
print "Number of Characters removed : $A";
输出:
Chopped String is : GfG is a computer science portal
Number of Characters removed : 1
在上面的代码中,可以看到输入字符串包含一个字符(\n),该换行符被 chomp()函数删除。
示例 2:
#!/usr/bin/perl
# Initialising two strings
$string1 = "Geeks for Geeks\n\n";
$string2 = "Welcomes you all\n";
# Calling the chomp() function
$A = chomp($string1, $string2);
# Printing the chopped string and
# removed trailing newline character
print "Chopped String is : $string1$string2\n";
print "Number of Characters removed : $A\n";
输出:
Chopped String is : Geeks for Geeks
Welcomes you all
Number of Characters removed : 2
在上面的代码中,可以看到输入 string1 包含两个字符(\n\n),其中只有最后一个字符被 chomp()函数删除,并使用倒数第二个字符,可以在输出。