Extract Substring with indexOf : String Find Search « Data Type « Java Tutorial






public class ExtractSubstring {
  public static void main(String[] args) {
    String text = "To be or not to be";
    int count = 0;
    char separator = ' ';

    int index = 0;
    do {
      ++count;
      ++index;
      index = text.indexOf(separator, index);
    } while (index != -1);

    String[] subStr = new String[count];
    index = 0;
    int endIndex = 0;
    for (int i = 0; i < count; i++) {
      endIndex = text.indexOf(separator, index);
      if (endIndex == -1)
        subStr[i] = text.substring(index);
      else
        subStr[i] = text.substring(index, endIndex);
      index = endIndex + 1;
    }

    for (int i = 0; i < subStr.length; i++)
      System.out.println(subStr[i]);
  }
}








2.29.String Find Search
2.29.1.Find the latest index of any of a set of potential substrings.
2.29.2.Find the first index of any of a set of potential substrings.
2.29.3.Finds the first index within a String, handling null.
2.29.4.Finds the last index within a String from a start position, handling null.
2.29.5.Finds the n-th index within a String, handling null.
2.29.6.Use String.indexOf to locate a character in a string
2.29.7.Use String.lastIndexOf to find a character in a string
2.29.8.Use String.indexOf to locate a substring in a string
2.29.9.Use lastIndexOf to find a substring in a string
2.29.10.Demonstrate indexOf() and lastIndexOf().
2.29.11.Extract Substring with indexOf
2.29.12.Java String endsWith
2.29.13.Java String startsWith
2.29.14.Starts with, ignore case( regular expressions )
2.29.15.Ends with, ignore case( regular expressions )
2.29.16.Anywhere, ignore case( regular expressions )
2.29.17.Last occurrence of a character
2.29.18.Not found returns -1
2.29.19.A bubble sort for Strings.
2.29.20.Search a String to find the first index of any character in the given set of characters.
2.29.21.Search a String to find the first index of any character not in the given set of characters.