chop函数_Perl数组chop()和chomp()函数-快速教程

chop函数

Perl's chop and chomp functions can often be a source of confusion. Not only do they sound similar, they do similar things. Unfortunately, there is a critical difference—​chop removes the last character of the string completely, while chomp only removes the last character if it is a newline.

Perl的 chopchomp功能通常会引起混乱。 他们不仅听起来相似,而且做类似的事情。 不幸的是,有一个关键difference- 删除的最后一个字符的字符串完全,而格格只删除最后一个字符,如果它是一个换行符

Chomping $myName cuts off the last newline, leaving just Jacob. Once it's been chomped, further chomping won't do anything at all. Chopping the name, however, will result in the last character being removed, leaving Jaco:

砍掉$ myName会切断最后一个换行符,只剩下Jacob 。 一旦被切碎,进一步的切碎根本不会做任何事情。 但是, 砍掉名字将导致最后一个字符被删除,留下Jaco

Chomping and chopping an array results each element being acted on, and can be a real time saver.

格格荷兰国际集团和CHOP平安阵列结果上所作用的每个元素,并可以是一个真正的节省时间。

So remember - Chop chops off the last character without question or regret. Chomp only removed the newline, leaving the string itself intact. Chomp does not remove all whitespace characters by default. In fact, by default, chomp only removes what is currently defined as the $INPUT_RECORD_SEPARATOR. If your goal is to trim all whitespace from the end of your string, try using a regex like this one submitted by a reader:

所以请记住- 斩掉最后一个字符,毫无疑问或后悔。 Chomp仅删除了换行符,而字符串本身保持不变。 默认情况下, Chomp不会删除所有空格字符。 实际上,默认情况下,chomp仅删除当前定义为$ INPUT_RECORD_SEPARATOR的内容 。 如果您的目标是修剪字符串末尾的所有空白,请尝试使用由读者提交的正则表达式,例如:

翻译自: https://www.thoughtco.com/perl-array-chop-chomp-function-tutorial-2641180

chop函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值