码迷,mamicode.com
首页 > 其他好文 > 详细

Ruby 去掉结尾空格

时间:2015-07-24 18:38:56      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

有几个方法可选

  • strip() #对字符串中间的空白不做任何处理
 irb(main):012:0> " \tHello \n world.\r\n  ".strip
=> "Hello \n world."
irb(main):013:0> " \tHello \n world.\r\n  ".lstrip
=> "Hello \n world.\r\n  "
irb(main):014:0> " \tHello \n world.\r\n  ".rstrip
=> " \tHello \n world."

*gsub() #用正则表达式全字符串匹配

irb(main):020:0> " \tHello \n world.\r\n  ".gsub(/\s+/,‘‘)
=> "Helloworld."
irb(main):021:0> " \tHello \n world.\r\n  ".gsub(‘ ‘,‘‘)
=> "\tHello\nworld.\r\n"
irb(main):023:0> " \tHello \n world.\r\n  ".gsub("\n",‘‘)
=> " \tHello  world.\r  "
irb(main):024:0> " \tHello \n world.\r\n  ".gsub("\n",‘‘).gsub("\r",‘‘)
=> " \tHello  world.  "

版权声明:本文为博主原创文章,未经博主允许不得转载。

Ruby 去掉结尾空格

标签:

原文地址:http://blog.csdn.net/u010713545/article/details/47041233

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!