仅删除字符串开头的空白字符
编辑:我通过使用 strip!
解决了这个问题 删除前导和尾随空格正如我在该视频中展示的那样。然后,我通过迭代并添加空格来恢复数组每个字符串末尾的空格。
这个问题与“欺骗”不同,因为我的目的是保留末尾的空格。然而,脱衣!如果您愿意的话,将删除前导和尾随空格。
我有一个单词数组,我试图删除单词开头可能存在的任何空格,而不是结尾处的空格。 rstrip!只处理字符串的结尾。我想从字符串的开头删除空格。
example_array = ['peanut', ' butter', 'sammiches ']
desired_output = ['peanut', 'butter', 'sammiches ']
正如您所看到的,并非数组中的所有元素都存在空格问题,因此我不能像所有元素都以单个空格字符开头那样删除第一个字符。
完整代码:
words = params[:word].gsub("\n", ",").delete("\r").split(",")
words.delete_if {|x| x == ""}
words.each do |e|
e.lstrip!
end
用户可以在表单上输入的示例文本:
Corn on the cob,
Fibonacci,
StackOverflow
Chat, Meta, About
Badges
Tags,,
Unanswered
Ask Question
Edit: I solved this by using strip!
to remove leading and trailing whitespaces as I show in this video. Then, I followed up by restoring the white space at the end of each string the array by iterating through and adding whitespace.
This problem varies from the "dupe" as my intent is to keep the whitespace at the end. However, strip! will remove both the leading and trailing whitespace if that is your intent.
I have an array of words where I am trying to remove any whitespace that may exist at the beginning of the word and not ones at the end. rstrip! just takes care of the end of a string. I want whitespaces removed from the beginning of a string.
example_array = ['peanut', ' butter', 'sammiches ']
desired_output = ['peanut', 'butter', 'sammiches ']
As you can see, not all elements in the array have the whitespace problem, so I can't just delete the first character as I would if all elements started with a single whitespace character.
Full code:
words = params[:word].gsub("\n", ",").delete("\r").split(",")
words.delete_if {|x| x == ""}
words.each do |e|
e.lstrip!
end
Sample text that a user may enter on the form:
Corn on the cob,
Fibonacci,
StackOverflow
Chat, Meta, About
Badges
Tags,,
Unanswered
Ask Question
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
String#lstrip
(或String#lstrip!
)是你在追求什么。关于您的代码的更多评论:
delete_if {|x| x == ""}
可以替换为delete_if(&:empty?)
reject!
因为delete_if
只会返回一个不同的数组,而不是修改现有的数组。words.each {|e| e.lstrip!}
可以替换为words.each(&:lstrip!)
delete("\r")
应该是多余的,如果你'在 Windows 机器上读取 Windows 风格的文本文档,或在 Unix 机器上阅读 Unix 风格的文档split(",")
可以替换为split(", ")< /code> 或
split(/, */)
(或/, ?/
如果最多有一个空格)所以现在看起来像:
我会如果您有可用的示例文本,能够提供更多建议。
编辑:好的,这里是:
使用的方法是
String#split
,Enumerable#map
,Enumerable#reject
和Array#flatten
。Ruby 也有用于解析逗号分隔文件的库,但我认为它们在 1.8 和 1.9 之间有点不同。
String#lstrip
(orString#lstrip!
) is what you're after.More comments about your code:
delete_if {|x| x == ""}
can be replaced withdelete_if(&:empty?)
reject!
becausedelete_if
will only return a different array, rather than modify the existing one.words.each {|e| e.lstrip!}
can be replaced withwords.each(&:lstrip!)
delete("\r")
should be redundant if you're reading a windows-style text document on a Windows machine, or a Unix-style document on a Unix machinesplit(",")
can be replaced withsplit(", ")
orsplit(/, */)
(or/, ?/
if there should be at most one space)So now it looks like:
I'd be able to give more advice if you had the sample text available.
Edit: Ok, here goes:
The methods used are
String#split
,Enumerable#map
,Enumerable#reject
andArray#flatten
.Ruby also has libraries for parsing comma seperated files, but I think they're a little different between 1.8 and 1.9.
去掉两个空格...
Strips both white spaces...
编辑:添加了 gsub 替代方案。
为什么不将 gsub 与正则表达式一起使用:
这会将输入开头的空格替换为空字符串。有关详细信息,请参阅 https://apidock.com/ruby/String/gsub
code>strip 方法很优雅,但它删除了字符串开头和结尾的空格。
有关信息,请参阅 https://apidock.com/ruby/String/strip
EDIT: a gsub alternative added.
Why not use gsub with regex:
This will replace whitespace at the beginning of the input with an empty string. For more info, see https://apidock.com/ruby/String/gsub
The
strip
method is elegant but it removes whitespace from both the beginning and end of string.For info, see https://apidock.com/ruby/String/strip