我有以下字符串:
this sentence: should be: split after last colon: sentence
我想把上面的字符串按最后一个冒号(:)分割,以便得到这两个元素:
[ "this sentence: should be: split after last colon", "sentence" ]
["this sentence: should be: splited after last colon:", "sentence"]
我有以下字符串:
this sentence: should be: split after last colon: sentence
我想把上面的字符串按最后一个冒号(:)分割,以便得到这两个元素:
[ "this sentence: should be: split after last colon", "sentence" ]
["this sentence: should be: splited after last colon:", "sentence"]
尝试简单的代码:
s = 'this sentence: should be: splited after last colon: sentence'
s =~ /(.*):(.*)?/
[ $1 << ':', $2 ]
# => ["this sentence: should be: splited after last colon:", " sentence"]
试一试
str = "this sentence: should be: splited after last colon: sentence"
last_pos = str.rindex(/\:/)
arr = [str[0..last_pos].strip, str[last_pos + 1 .. str.length].strip]
=>["this sentence: should be: splited after last colon:", "sentence"]