0

I'm writing a program with the below requirement.

I've a string as (Ravi) has good knowledge in (Java)

and from this string I need to get the index values of ( and ).

I know how to get the first ( using indexOf() and the last ), getLastIndexOf(), but how can I get index values of ( and ) in (Ravi) and (Java)

I'm able to get the values using

List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("\\((.*?)\\)");
Matcher regexMatcher = regex.matcher(line);
while (regexMatcher.find()) {
    matchList.add(regexMatcher.group(1));
}
for (String str : matchList) {
 utterancesSet.add(str);
}
System.out.println(utterancesSet);

But here, I need the index values, not the strings

Thanks

user3872094
  • 3,269
  • 8
  • 33
  • 71
  • Have you tried iterating over all character of the string and using a List? – Anand Undavia Mar 21 '17 at 09:59
  • there is also indexOf with two arguments... use it in a loop – vefthym Mar 21 '17 at 10:00
  • 1
    Use the `indexOf(String, int)` version that takes a starting position parameter : https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String,%20int) – Arnaud Mar 21 '17 at 10:00
  • 1
    You can have the answer [there](http://stackoverflow.com/questions/4988050/java-indexof-method-for-multiple-matches-in-string). – Dimitri Bosteels Mar 21 '17 at 10:02

2 Answers2

2
public int indexOf(String str, int fromIndex)

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

The returned index is the smallest value k for which:

k >= fromIndex  && this.startsWith(str, k)

If no such value of k exists, then -1 is returned.

Parameters:

str - the substring to search for.

fromIndex - the index from which to start the search.

Returns:

the index of the first occurrence of the specified substring, starting at the specified index, or -1 if there is no such occurrence.

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#indexOf-java.lang.String-int-

Harmlezz
  • 7,972
  • 27
  • 35
2

Use indexOf in a for loop, like this :

String text = "0123hello9012hello8901hello7890";
String word = "hello";

System.out.println(text.indexOf(word)); // prints "4"
System.out.println(text.lastIndexOf(word)); // prints "22"

// find all occurrences
for (int i = -1; (i = text.indexOf(word, i + 1)) != -1; ) {
    System.out.println(i);
} // prints "4", "13", "22"

For more information on this example : Java: method to get position of a match in a String?

Community
  • 1
  • 1
Antoine
  • 181
  • 1
  • 8
  • 1
    why copy another answer, instead of providing the link as a comment ? (http://stackoverflow.com/a/2615834/2516301) – vefthym Mar 21 '17 at 10:02
  • You're right, it's another way of doing it. But at least he have the answer (and example) on the same page now ^^ – Antoine Mar 21 '17 at 10:05
  • 1
    Welcome to SO Antoine! If you copy other answers, at least provide the link to where you got them from, otherwise it looks like you thought about it, while someone else did, in reality. This would give credit to the one who spent some time to think about it – vefthym Mar 21 '17 at 10:07
  • Ok sorry, you're right. I'll do it now. – Antoine Mar 21 '17 at 10:08
  • 1
    not exactly what I meant, but much better now. I retract my downvote. – vefthym Mar 21 '17 at 10:11