Perl chomp 函式



說明

這個 chomp 的更安全的版本刪除任何與 $/ 當前值(在 English 模組中也稱為 $INPUT_RECORD_SEPARATOR)相對應的尾隨字串。它返回從其所有引數中刪除的字元總數。預設情況下,$/ 被設定為新行字元。

語法

以下是此函式的簡單語法 -

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
perl_function_references.htm
廣告
© . All rights reserved.