Find indexes of all occurrences of character in a String in Java
This post will discuss how to find indexes of all occurrences of a character in a string in Java.
1. Using indexOf() and lastIndexOf() method
The String class provides an indexOf() method that returns the index of the first appearance of a character in a string.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Main { public static void main(String[] args) { String str = "C, C++, Java, C#, Kotlin"; char ch = ','; int index = str.indexOf(ch); System.out.println(index); // 1 } } |
To get the indices of all occurrences of a character in a String, you can repeatedly call the indexOf() method within a loop. The following example demonstrates how to use the indexOf() method efficiently, where the search for the next index starts from the previous index.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Main { public static void main(String[] args) { String str = "C, C++, Java, C#, Kotlin"; char ch = ','; int index = str.indexOf(ch); while (index != -1) { System.out.println(index); index = str.indexOf(ch, index + 1); } } } |
Output:
1
6
12
16
Or even shorter:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Main { public static void main(String[] args) { String str = "C, C++, Java, C#, Kotlin"; char ch = ','; int index = -1; while ((index = str.indexOf(ch, index + 1)) != -1) { System.out.println(index); } } } |
Output:
1
6
12
16
If you just need the index of the character’s last appearance in a string, use the lastIndexOf() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Main { public static void main(String[] args) { String str = "C, C++, Java, C#, Kotlin"; char ch = ','; int lastIndex = str.lastIndexOf(ch); System.out.println(lastIndex); // 16 } } |
2. Using IntStream.iterate() method
With Java 9, you can use the IntStream.iterate(seed, hasNext, next) method which returns a sequential ordered IntStream produced by application of the next function to an initial element seed, conditioned on satisfying the hasNext predicate. Following is a simple example demonstrating usage of this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { String str = "C, C++, Java, C#, Kotlin"; char ch = ','; List<Integer> indices = IntStream.iterate(str.indexOf(ch), i -> i != -1, i -> str.indexOf(ch, i + 1)) .boxed().collect(Collectors.toList()); System.out.println(indices); } } |
Output:
[1, 6, 12, 16]
3. Using Regex
To get the indexes of all appearances of a character in a String, you can also use a regex. The idea is to create a matcher to match the string for the specified character. Here’s how the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { String str = "C, C++, Java, C#, Kotlin"; String ch = ","; Matcher matcher = Pattern.compile(ch).matcher(str); while (matcher.find()) { System.out.println(matcher.start()); } } } |
Output:
1
6
12
16
That’s all about finding indexes of all occurrences of a character in a string in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)