'how to get index by keyword in a string?

if I have a String:

String str = "Hello, World!";

how can I get index of a key word:

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: I don't want to use for loop iterate String, because it's too slow



Solution 1:[1]

int begin_index = str.undexOf("World");

int end_index = begin_index+"World".length();

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ptk