ruby 如何通过拆分字符的最后一次出现将字符串拆分为仅两部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12192343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to split a string into only two parts, by the last occurrence of the split char?
提问by ohho
For example:
例如:
"Angry Birds 2.4.1".split(" ", 2)
=> ["Angry", "Birds 2.4.1"]
How can I split the string into: ["Angry Birds", "2.4.1"]
如何将字符串拆分为: ["Angry Birds", "2.4.1"]
回答by Vadym Tyemirov
String#rpartition, e.g.
irb(main):068:0> str = "Angry Birds 2.4.1"
=> "Angry Birds 2.4.1"
irb(main):069:0> str.rpartition(' ')
=> ["Angry Birds", " ", "2.4.1"]
Since the returned value is an array, using .firstand .lastwould allow to treat the result as if it was split in two, e.g
由于返回值是一个数组,使用。首先和。去年将允许治疗结果,如果它是一分为二,例如
irb(main):073:0> str.rpartition(' ').first
=> "Angry Birds"
irb(main):074:0> str.rpartition(' ').last
=> "2.4.1"
回答by halfelf
I hava a solution like this:
我有一个这样的解决方案:
class String
def split_by_last(char=" ")
pos = self.rindex(char)
pos != nil ? [self[0...pos], self[pos+1..-1]] : [self]
end
end
"Angry Birds 2.4.1".split_by_last #=> ["Angry Birds", "2.4.1"]
"test".split_by_last #=> ["test"]
回答by oldergod
Something like this maybe ? Split where a space is followed by anything but a space till the end of the string.
也许像这样的事情?拆分,其中空格后跟除空格以外的任何内容直到字符串末尾。
"Angry Birds 2.4.1".split(/ (?=\S+$)/)
#=> ["Angry Birds", "2.4.1"]
回答by pvandenberk
I don't seem able to get the example code in my comment properly formatted, so I'm submitting it as a separate answer, even though Vadym Tyemirovdeserves all the credit for the String#rpartitionsolution he provided above.
我似乎无法在我的评论中获得正确格式化的示例代码,所以我将其作为单独的答案提交,即使Vadym Tyemirov值得称赞String#rpartition他在上面提供的解决方案。
I just wanted to add that String#rpartitionplays very nicely with Ruby's "don't care" variable, as typically you're indeed only interested in the first and last element of the result array, but not the middle element (the separator):
我只是想补充一点,它String#rpartition与 Ruby 的“don't care”变量非常相配,因为通常您确实只对结果数组的第一个和最后一个元素感兴趣,而不对中间元素(分隔符)感兴趣:
[1] pry(main)> name, _, version = "Angry Birds 2.4.1".rpartition(' ')
=> ["Angry Birds", " ", "2.4.1"]
[2] pry(main)> name
=> "Angry Birds"
[3] pry(main)> version
=> "2.4.1"
So no need for Array#firstor Array#last... less is more! :-)
所以不需要Array#first或Array#last......少即是多!:-)
回答by matthew.tuck
This is probably way too tricky (and probably not particularly efficient), but you can do this:
这可能太棘手了(并且可能不是特别有效),但您可以这样做:
"Angry Birds 2.4.1".reverse.split(" ", 2).map(&:reverse).reverse
回答by jsarma
The rpartition solution makes a great sexy one-liner (I voted for it), but here's another technique if you want a one liner that's more flexible for solving more complex partitioning problems:
rpartition 解决方案是一个非常性感的单行(我投了赞成票),但是如果您想要一个更灵活的单行来解决更复杂的分区问题,这是另一种技术:
["Angry Birds 2.4.1".split(' ')[0..-2].join(' '), "Angry Birds 2.4.1".split(' ')[-1..-1].join(' ')]
By more flexible, I mean if there were more items being partitioned, you could just adjust the range of the sequence.
更灵活,我的意思是如果有更多的项目被分区,你可以调整序列的范围。
回答by sumskyi
"Angry Birds 2.4.1".split(/ (?=\d+)/)
"Angry Birds 2.4.1".split(/ (?=\d+)/)
回答by Jing Li
class String
def divide_into_two_from_end(separator = ' ')
self.split(separator)[-1].split().unshift(self.split(separator)[0..-2].join(separator))
end
end
"Angry Birds 2.4.1".divide_into_two_from_end(' ') #=> ["Angry Birds", "2.4.1"]

