简体   繁体   中英

how to get the last match in a string builder in java

i have a string builder like this

"abc efg abc hij abc klm"

i want to get the last abc from this string and also the data's following the last match. How to do this in java?

 StringBuilder text = new StringBuilder("abc efg abc hij abc klm");
 int index = text.lastIndexOf("abc");
 String substring = text.toString().substring(index);

As follows:

StringBuilder buffer = new StringBuilder();
buffer.append("abc efg abc hij abc klm");
int lastIndex = buffer.lastIndexOf("abc");
if (lastIndex >= 0) {
    System.out.println(buffer.substring(lastIndex));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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