package com.mindprod.example;

import static java.lang.System.out;

/**
 * Exercise String.indexOf
 * composed with IntelliJ IDEA
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.1
 */
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" ) );

        }

    }