获取字符串中间的索引

时间:2014-09-26 02:19:18

标签: groovy

我有一个像这样的字符串

def list1 = "magazine, news, friend, Steve, other: movie"
def list2 = "cartoon, music, magazine, news, friend, Peter, other: movie"

如何获取朋友的索引,文本可能会更改,但friend不会更改。史蒂夫是朋友的名字,可能是杰克或安迪或者某个名字。 任何解决方案将不胜感激。感谢

1 个答案:

答案 0 :(得分:3)

def list1 = "magazine, news, friend, Steve, other: movie"
def list2 = "cartoon, music, magazine, news, friend, Peter, other: movie"

assert [list1, list2].collect { it.tokenize(/,/)*.trim().indexOf('friend') } == [2, 4]