我需要在句子的最后一个单词之前添加一个单词。
示例:
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"
在最后一句话之前应该是单词。
答案 0 :(得分:3)
t1.split(" ").insert(-2,t2).join(" ")
答案 1 :(得分:2)
a = 'first third'
a.split(' ').insert(-2, 'second').join(' ')
=> "first second third"
a = 'first'
a.split(' ').insert(-2, 'second').join(' ')
=> "second first"