问题描述
我在那里我试图去除可能在单词的开头,而不是在年底存在任何空白字的数组。 rstrip!只是需要一个字符串的结束照顾。
example_array = ['花生','黄油','sammiches']
desired_output = ['花生','黄油','sammiches']
正如你所看到的,而不是数组中的所有元素有空白的问题,所以我不能只是删除第一个字符,我会在所有的元素开始用一个空格字符。
全部code:
字=参数[:字] .gsub(\\ n,)删除(\\ R)分裂()。
words.delete_if {| X |点¯x==}
words.each做| E |
e.lstrip!
结束
这是一个用户可以在表单上输入文字示例:
(我把它放在一个code的thingie显示它可能究竟是如何进入,不知道为什么有些是蓝色的,有些不..只是pretend它的所有相同的颜色。)
玉米棒,
斐波那契数,
堆栈溢出
聊天,梅塔,关于
徽章
标签,,
悬而未决
问问题
字符串#lstrip
(或字符串#lstrip!
)是后在做什么。
desired_output = example_array.map(安培;:lstrip)
你的code的评论:
-
delete_if {| X |点¯x==}
可以用delete_if更换(安培;:?空)
- 除非你想
拒绝!
,因为delete_if
只会返回一个不同的数组,而不是修改现有的。 -
words.each {| E | !e.lstrip}
可以用words.each更换(安培;:lstrip!)
-
删除(\\ R)
如果你正在读一个Windows机器上的Windows风格的文本文档,或在Unix风格的文件应该是多余的Unix机器 -
拆分()
可以用替换拆分()
或斯普利特(/ * /)
(或/,/?
是否应该有最多一个空格)
所以,现在它看起来像:
字=参数[:字]。.gsub(\\ n,)分裂(?/,/)
words.reject!(安培;:空的?)
words.each(安培;:lstrip!)
我能给予更多的意见,如果你有可用的示例文本。
修改:好吧,这里有云:
temp_array = text.split(\\ n)地图做|线|
域= line.split(/ * /)
non_empty_fields = fields.reject(安培;:空的?)
结束
temp_array.flatten(1)
使用的方法是字符串#分裂
,可枚举#地图
,可枚举#拒绝
和阵列#压扁
。
Ruby也有解析逗号分隔的文件库,但我认为他们是在1.8和1.9之间有一点不同。
I have an array of words where I am trying to remove any whitespace that may exist at the beginning of the word instead of at the end. rstrip! just takes care of the end 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 char.
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:(I put it in a code thingie to show exactly how it might be entered, not sure why some is blue and some is not.. just pretend it's all the same color.)
Corn on the cob,
Fibonacci,
StackOverflow
Chat, Meta, About
Badges
Tags,,
Unanswered
Ask Question
String#lstrip
(or String#lstrip!
) is what you're after.
desired_output = example_array.map(&:lstrip)
More comments about your code:
delete_if {|x| x == ""}
can be replaced withdelete_if(&:empty?)
- Except you want
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:
words = params[:word].gsub("\n", ",").split(/, ?/)
words.reject!(&:empty?)
words.each(&:lstrip!)
I'd be able to give more advice if you had the sample text available.
Edit: Ok, here goes:
temp_array = text.split("\n").map do |line|
fields = line.split(/, */)
non_empty_fields = fields.reject(&:empty?)
end
temp_array.flatten(1)
The methods used are String#split
, Enumerable#map
, Enumerable#reject
and Array#flatten
.
Ruby also has libraries for parsing comma seperated files, but I think they're a little different between 1.8 and 1.9.
这篇关于红宝石:在字符串的开头删除空格字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!