String Characters : Extracting String Characters « Data Type « Java Tutorial






public class StringCharacters {
  public static void main(String[] args) {
    String text = "To be or not to be?";

    int spaces = 0,
    vowels = 0, 
    letters = 0;

    for (int i = 0; i < text.length(); i++) {
      char ch = Character.toLowerCase(text.charAt(i));
      if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
        ++vowels;
      if (Character.isLetter(ch))
        ++letters;
      if (Character.isWhitespace(ch))
        ++spaces;
    }
  }
}








2.25.Extracting String Characters
2.25.1.Searching Strings for Characters
2.25.2.Finding the last occurrence of 'a' in the String variable text: the method lastIndexOf()
2.25.3.To search forwards from a given position: startIndex
2.25.4.Searching for Substrings
2.25.5.Last index of
2.25.6.Up to the end of the string
2.25.7.Beyond the last character of the substring
2.25.8.String Characters