2012-10-09 87 views
-1

我需要在最後一個單詞的單詞之前添加一個單詞。在ruby的最後一個單詞之前添加一個單詞

例子:

t1 = "Top Senior Players" OR t1 = "Top Athlete" OR t1 = "Youngest Athlete" 
    t2 = "Hockey" OR t2 = "Cricket" etc. 

我希望得到輸出如下

var game = "Top Senior <t2> Players" OR t1 = "Top <t2> Athlete" OR t1 = "Youngest <t2> Athlete" 

它應該是,在字的最後一個字之前。

回答

2
a = 'first third' 
a.split(' ').insert(-2, 'second').join(' ') 
=> "first second third" 

a = 'first' 
a.split(' ').insert(-2, 'second').join(' ') 
=> "second first" 
3
t1.split(" ").insert(-2,t2).join(" ") 
相關問題