【发布时间】:2012-12-30 16:20:45
【问题描述】:
可能重复:
find the nth occurence of a substring in a string in java?
有没有办法在字符串中获得特定的匹配项?
如果我们有String test = "this is a long test which is a test of a test";,那么我们可以使用Matcher 来获取test 的第二个(或任何特定)实例吗?
我以为我可以使用Matcher.find(x),但这似乎不太好用...
【问题讨论】:
-
@Threat 是否要查找包含单词?
-
如果您要查找的是文字字符串,则只需使用 indexOf。如果您想找到与正则表达式匹配的第二次出现,那么您还可以使用带有计数器的 Matcher 循环。也可以先使用 Pattern/Matcher 进行查找。
-
@nhahtdh 你也可以在循环中使用
indexOf(char, int)。 -
@MarkoTopolnik:String 类中没有具有该签名的方法。
标签: java regex string pattern-matching match