简体   繁体   English

从右到左获取字符串中子字符串的下一个匹配项(Java)

[英]Get next occurrence from RIGHT TO LEFT of a substring in a string (Java)

Say I have a string s like this: 说我有一个字符串s是这样的:

s = "... *begin* - some-code-here - *end* ..."
i:                    ^

and an index i pointing to a position between "*begin*" and "*end*" , eg, to the 'e' of "some" . 索引i指向"*begin*""*end*"之间的位置,例如"some"'e' "some"

To get the index of the (next) occurrence of "*end"* , I could use 要获取"*end"* (下一个)出现的索引,我可以使用

s.indexOf("*end*", i)

Is there a similar way to get the index of the next "*begin*" to the left? 是否有类似的方法可以将下一个"*begin*"的索引移到左侧?

采用

s.lastIndexOf("*begin*", i);

I want this answer to add as comment to wero's answer but due to low reputation I am unable to do so. 我希望此答案添加到wero答案的注释中,但由于声誉低下,我无法这样做。

I think it should be like 我认为应该像

s = s.substring(0, i);

s.indexOf("*begin");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM