/* * @(#)TestIndexOf.java * * Summary: Demonstrate use of String.indexOf. * * Copyright: (c) 2009 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.6+ * * Created with: IntelliJ IDEA IDE. * * Version History: * 1.1 2009-04-20 - tidy comments. */ package com.mindprod.example; import static java.lang.System.out; /** * Demonstrate use of String.indexOf. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2009-04-20 - tidy comments. * @since 2009 */ public final class TestIndexOf { // --------------------------- main() method --------------------------- /** * Test harness * * @param args not used */ public static void main( String[] args ) { // use of indexOf final String s1 = "ABCDEFGABCDEFG"; // prints 2, 0-based offset of first CD where found. out.println( s1.indexOf( "CD" ) ); // prints -1, means not found, search is case sensitive out.println( s1.indexOf( "cd" ) ); // prints 2, 0-based offset of first cd where found out.println( s1.toLowerCase().indexOf( "cd" ) ); // prints 2, 0-based offset of first cd where found out.println( s1.indexOf( "cd".toUpperCase() ) ); // prints 9, 0-based offset relative to the original string, // not relative to the start of the substring out.println( s1.indexOf( "CD", 4/* start looking here, after the first CD */ ) ); // use of last indexOf // prints 9, 0-based offset of where last CD found. out.println( s1.lastIndexOf( "CD" ) ); // prints -1, means not found, search is case sensitive out.println( s1.lastIndexOf( "cd" ) ); // prints 9, 0-based offset of where last cd found out.println( s1.toLowerCase().lastIndexOf( "cd" ) ); // prints 9, 0-based offset of where last cd found out.println( s1.lastIndexOf( "cd".toUpperCase() ) ); // prints 2, 0-based offset relative to the original string, // not relative to the start of the substring out.println( s1.lastIndexOf( "CD", 8/* start looking here, prior to last */ ) ); // German ss problem with case-insensitive searching: // prints German ss ligature Esszet, ß, one char that looks a bit like a beta. out.println( "ß" ); // prints SS, two chars long! out.println( "ß".toUpperCase() ); // prints 5 instead of the expected 4 out.println( "grüßen".toUpperCase().indexOf( "E" ) ); } }