如何将一个字符串分割成两个部分,直到最后出现分割字符为止?

时间:2021-05-06 21:38:56

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"]

如何将字符串分割成:["愤怒的小鸟","2.4.1"]

8 个解决方案

#1


67  

String#rpartition, e.g.

字符串# rpartition,如。

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 .first and .last would allow to treat the result as if it was split in two, e.g

由于返回的值是一个数组,所以使用.first和.last可以将结果当作被分割成两部分,例如

irb(main):073:0> str.rpartition(' ').first
=> "Angry Birds"
irb(main):074:0> str.rpartition(' ').last
=> "2.4.1"

#2


9  

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"]

#3


6  

Some 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"]

#4


1  

"Angry Birds 2.4.1".split(/ (?=\d+)/)

“愤怒的小鸟”2.4.1。分割(/(? = \ d +)/)

#5


1  

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

#6


1  

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 Tyemirov deserves all the credit for the String#rpartition solution he provided above.

我似乎无法在我的注释中获得正确格式的示例代码,所以我将它作为一个单独的答案提交,尽管Vadym Tyemirov为他提供的字符串#rpartition解决方案获得了所有荣誉。

I just wanted to add that String#rpartition plays 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):

我只是想添加一个字符串#rpartition与Ruby的“不关心”变量很好地结合在一起,通常情况下,您只对结果数组的第一个和最后一个元素感兴趣,而不是中间元素(分隔符):

[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#first or Array#last... less is more! :-)

所以不需要数组#first或数组#last…少即是多!:-)

#7


1  

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.

说到更灵活,我的意思是如果有更多的项被分割,你可以调整序列的范围。

#8


0  

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"]

#1


67  

String#rpartition, e.g.

字符串# rpartition,如。

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 .first and .last would allow to treat the result as if it was split in two, e.g

由于返回的值是一个数组,所以使用.first和.last可以将结果当作被分割成两部分,例如

irb(main):073:0> str.rpartition(' ').first
=> "Angry Birds"
irb(main):074:0> str.rpartition(' ').last
=> "2.4.1"

#2


9  

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"]

#3


6  

Some 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"]

#4


1  

"Angry Birds 2.4.1".split(/ (?=\d+)/)

“愤怒的小鸟”2.4.1。分割(/(? = \ d +)/)

#5


1  

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

#6


1  

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 Tyemirov deserves all the credit for the String#rpartition solution he provided above.

我似乎无法在我的注释中获得正确格式的示例代码,所以我将它作为一个单独的答案提交,尽管Vadym Tyemirov为他提供的字符串#rpartition解决方案获得了所有荣誉。

I just wanted to add that String#rpartition plays 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):

我只是想添加一个字符串#rpartition与Ruby的“不关心”变量很好地结合在一起,通常情况下,您只对结果数组的第一个和最后一个元素感兴趣,而不是中间元素(分隔符):

[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#first or Array#last... less is more! :-)

所以不需要数组#first或数组#last…少即是多!:-)

#7


1  

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.

说到更灵活,我的意思是如果有更多的项被分割,你可以调整序列的范围。

#8


0  

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"]