如何通过字符串中的关键字获取索引?

how to get index by keyword in a string?

如果我有一个字符串:

String str = "Hello, World!";

如何获取关键字的索引:

int begin_index = str.GetKeywordIndex("World").GetBeginIndex();
int end_index = str.GetkeyWordIndex("World").GetEndIndex();

System.out.println("keyword World begin at:" + begin_index + "end: " + end_index);

// keyword World begin at: 7, end: 11

ps:不想用for循环迭代String,因为太慢了

int begin_index = str.undexOf("世界");

int end_index = begin_index+"世界".length();

System.out.println("关键字世界开始于:" + begin_index + "结束:" + end_index);