有几个方法可选
- 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. "
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-25 12:27:16