About 16,900,000 results
Open links in new tab
  1. java - Indexes of all occurrences of character in a string - Stack Overflow

    public static int[] indexesOf(String s, String flag) { int flagLen = flag.length(); String current = s; int[] res = new int[s.length()]; int count = 0; int base = 0; while(current.contains(flag)) { int index = current.indexOf(flag); res[count] = index + base; base += index + flagLen; current = current.substring(current.indexOf(flag) + flagLen ...

  2. Java String indexOf() Method - W3Schools

    The indexOf() method returns the position of the first occurrence of specified character(s) in a string. Tip: Use the lastIndexOf method to return the position of the last occurrence of specified character(s) in a string.

  3. java - indexOf to find all occurrences of a word in a String - Stack ...

    Jul 27, 2016 · The indexOf(String str) method finds the index of the first occurrence of a str. So if you want to find all the occurrances of str then I'd suggest just implementing a loop in which you cut the string once you find an instance of str and look for str within theString again until it is no longer in theString .

  4. Find indexes of all occurrences of character in a String in Java

    Aug 13, 2022 · 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.

  5. java - Get string character by index - Stack Overflow

    The endIndex (second parameter) of String.substring(int, int) is an exclusive index, and it won't throw an exception for index + 1 as long as index < length() -- which is true even for the last character in the string. The method you're looking for is charAt. Here's an example: For more information, see the Java documentation on String.charAt.

  6. Using indexOf to Find All Occurrences of a Word in a String

    Jan 8, 2024 · In this tutorial, we’ll demonstrate a simple algorithm that uses the indexOf (String str, int fromIndex) method of the Java String class to find all occurrences of a word within a string. 2. Simple Algorithm.

  7. Java IndexOf: A Comprehensive Guide to Finding String

    In Java, the `IndexOf` method is a powerful tool for searching through strings to find the index of the first occurrence of a specified substring. This tutorial provides a comprehensive guide on how to utilize this method effectively, regardless of whether …

  8. String Searching Methods in Java - Java Guides

    The indexOf() method returns the index within the string of the first occurrence of the specified substring or character. It returns -1 if the substring or character is not found. public static void main(String[] args) { String str = "Hello, World!"; int index1 = str.indexOf('o'); int index2 = str.indexOf('o', 5);

  9. indexOf in Java – How to Find the Index of a String

    Aug 29, 2024 · The indexOf method in Java allows us to efficiently search strings to find the index position of characters or substrings. With its simple syntax, indexOf is one of the most frequently used string manipulation methods.

  10. Java String indexOf () - Find Character Index | Vultr Docs

    Dec 17, 2024 · When you need to find all occurrences of a character or substring, use indexOf() in a loop. Adjust the starting index on each iteration. This code continually searches for "Java" within the string, updating lastIndex each time "Java" is found to search beyond the current match. It effectively prints all starting positions of "Java" in the string.

  11. Some results have been removed
Refresh