Ruby:如何剥离字符串并获取删除的空格?
给定一个字符串,我想剥离
它,但我希望删除前后的空格。例如:
my_strip(" hello world ") # => [" ", "hello world", " "]
my_strip("hello world\t ") # => ["", "hello world", "\t "]
my_strip("hello world") # => ["", "hello world", ""]
您将如何实现 my_strip
?
Given a string, I would like to strip
it, but I want to have the pre and post removed whitespaces. For example:
my_strip(" hello world ") # => [" ", "hello world", " "]
my_strip("hello world\t ") # => ["", "hello world", "\t "]
my_strip("hello world") # => ["", "hello world", ""]
How would you implement my_strip
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
解决方案
测试套件 (RSpec)
Solution
Test Suite (RSpec)
好吧,这是我提出的一个解决方案:
但是,我想知道是否还有其他(也许更有效)的解决方案。
Well, this is a solution that I come up with:
But, I wonder if there are other (maybe more efficient) solutions.
我会使用正则表达式:
I would use regexp: