The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.lang  [58 examples] > Strings  [12 examples]

e74. Searching a String for a Character or a Substring

See also e423 Quintessential Regular Expression Search Program.
    String string = "madam, i am Adam";
    
    // Characters
    
        // First occurrence of a c
        int index = string.indexOf('a');    // 1
    
        // Last occurrence
        index = string.lastIndexOf('a');    // 14
    
        // Not found
        index = string.lastIndexOf('z');    // -1
    
    // Substrings
    
        // First occurrence
        index = string.indexOf("dam");      // 1
    
        // Last occurrence
        index = string.lastIndexOf("dam");  // 13
    
        // Not found
        index = string.lastIndexOf("z");    // -1

 Related Examples
e70. Constructing a String
e71. Comparing Strings
e72. Determining If a String Contains a Substring
e73. Getting a Substring from a String
e75. Replacing Characters in a String
e76. Replacing Substrings in a String
e77. Converting a String to Upper or Lower Case
e78. Converting a Primitive Type Value to a String
e79. Converting Between Unicode and UTF-8
e80. Determining a Character's Unicode Block
e81. Determining If a String Is a Legal Java Identifier

See also: Arrays    Assertions    Classes    Commands    Numbers    Objects    System Properties    Threads   


© 2002 Addison-Wesley.