Perl中的chomp()函數用於從輸入字符串中刪除最後一個尾隨的換行符。
用法: chomp(String)
參數:
String :輸入字符串,其尾隨的換行符將被刪除
返回值:從其所有參數中刪除的尾隨換行符的總數
示例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
在上麵的代碼中,可以看到,包含兩個換行符(\ n \ n)的輸入string1通過chomp()函數僅刪除了最後一個換行符,並使用了輸出中可以看到的倒數第二個換行符。
相關用法
- Perl uc()用法及代碼示例
- Perl abs()用法及代碼示例
- Perl int()用法及代碼示例
- Perl log()用法及代碼示例
- Perl oct()用法及代碼示例
- Perl ord()用法及代碼示例
- Perl exp用法及代碼示例
- Perl tell()用法及代碼示例
- Perl chr()用法及代碼示例
- Perl each()用法及代碼示例
- Perl sin()用法及代碼示例
- Perl cos()用法及代碼示例
- Perl hex用法及代碼示例
- Perl sprintf()用法及代碼示例
- Perl return()用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Perl | chomp() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。