$s1 = "hello\nhow are you\n"; $s2 = "I am fine\n"; print $s1; print $s2; chomp($s1); chomp($s2); print $s1; print $s2; # 输出: hello how are you I am fine hello # 字符串内部hello后面的换行符不会被去掉 how are youI am fineliu # $s1最后的换行符被去掉
# substr(str,initial_position,length) my $substr = substr('this is good day',0,4); print $substr; # output: this # 如果要输出之后的所有,省略第三个参数即可 my $substr = substr('this is good day',0); print $substr; # output:this is good day
如果第二个参数initial_position是-1,表示从倒数第一个开始提取,
1 2
my $substr = substr('this is good day',-3,3); print $substr; # output : day