The Java Developers Almanac 1.4


Order this book from Amazon.

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

e72. Determining If a String Contains a Substring

See also e423 Quintessential Regular Expression Search Program.
    String string = "Madam, I am Adam";
    
    // Starts with
    boolean  b = string.startsWith("Mad");  // true
    
    // Ends with
    b = string.endsWith("dam");             // true
    
    // Anywhere
    b = string.indexOf("I am") > 0;         // true
    
    // To ignore case, regular expressions must be used
    
    // Starts with
    b = string.matches("(?i)mad.*");
    
    // Ends with
    b = string.matches("(?i).*adam");
    
    // Anywhere
    b = string.matches("(?i).*i am.*");

 Related Examples
e70. Constructing a String
e71. Comparing Strings
e73. Getting a Substring from a String
e74. Searching a String for a Character or a Substring
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.