1. Strings and Characters
2. Processing Strings


Strings

So far we have used strings for simple tasks such as displaying a message on
the screen or specifying a color for a shape drawn on the canvas. 

A string is a sequence of characters. There is a special type in Java that
allows us to create string variables and to extract certain information.

Here are the basic operations:

Creating a string variable:

     String word = "hello";

     String droid = "R2D2";

     String empty = "";

Obtaining the length of a string:

     word.length()  --  returns 5

     droid.length()  --  returns 4

     empty.length() --  returns 0

Obtaining a character at a particular index:

     word.charAt( 0 )  --  returns 'h'
     word.charAt( 1 )  --  returns 'e'
     word.charAt( 4 )  --  returns 'o'

     droid.charAt( 2 ) --  returns 'D' 

     word.charAt( 6 ) --  error: invalid index

     droid.charAt( 5 ) --  error: invalid index

     empty.charAt( *any index* ) --  error: invalid index

Note: 
In Java (and many programming languages) the indexing starts at 0. This means
that the first character is at index 0, the second at index 1, and so on.
 
     String word = "hello";    
                 ___ ___ ___ ___ ___
     word -->   |_h_|_e_|_l_|_l_|_o_|
                  0   1   2   3   4     

     word length = 5

     first valid index = 0

     last valid index  = 4 = word.length() - 1

Note: 
The valid indices are the numbers that are smaller than the length of the string.
There is a special type in Java that allows us to create variables that hold only a single character:
char digit = '2';

char letter = 'a';

String word = "hello";

letter = word.charAt(0);      // letter is now 'h'

letter = word.charAt(4);      // letter is now 'o'

Note: 
* for char variables we enclose the value in single quotes

* for String variables we use double quotes
Examples on processing strings
Goal: Write a procedure printSpelling(String word) that takes a string and
displays the individual characters separated by a single space. 

For example:

    String word = "hello";

    printSpelling(word);     // displays:   h, e, l, l, o, 
Here is a possible description of the procedure in English: 1. keep track of the index of the current character 2. as long as the index is valid (i.e. less than the length of the string): 3. extract the current character 4. print the current character followed by comma and space 5. update the index Here is the Java translation:
void printSpelling( String phrase )
{
    int index = 0;                                 // (Step 1)

    while ( index < phrase.length() )              // (Step 2)
    {
        char curLetter = phrase.charAt( index );   // (Step 3)

        System.out.print( curLetter + ", " );      // (Step 4)

        index = index + 1;                         // (Step 5)
    }
    System.out.println();
}
Counting in a string
Goal: Write a procedure countCharacter(String phrase, char letter) that
computes how many times the given character occurs in the given string. 

For example:

    countCharacter( "helloworld", 'l' );     // returns 3

    countCharacter( "helloworld", 'a' );     // returns 0
Here is a possible description of the procedure in English: 1. keep track of the overall count for the desired character 2. keep track of the index of the current character 3. as long as the index is valid (i.e. less than the length of the string): 4. extract the current character 5. if the current character equals the given character: 6. update the overall count 7. update the index 8. return the count Here is the Java translation:
int countCharacter( String phrase, char letter )
{
    int result = 0;                                   // (Step 1)

    int index = 0;                                    // (Step 2)
    while ( index < phrase.length() )                 // (Step 3)
    {
        char curLetter = phrase.charAt( index );      // (Step 4)

        if ( curLetter == letter )                    // (Step 5)
        {
            result = result + 1;                      // (Step 6)
        }

        index = index + 1;                            // (Step 7)
    }

    return result;                                    // (Step 8)
}
Testing the code Here are possible ways to test the code. It is always a good idea to try the empty string and a one-character string, in addition to something more complicated to make sure that the code works in all cases:

Note that printSpelling is tested on its own since it is void, i.e. does not return a value, and cannot be place inside System.out.println .

public void run()
{
    printSpelling( "hello" );
    printSpelling( "a" );
    printSpelling( "" );

    System.out.println( "count l in helloworld: " + countCharacter("helloworld", 'l') );
    System.out.println( "count a in helloworld: " + countCharacter("helloworld", 'a') );

    System.out.println( "count a in a: " + countCharacter("a", 'a') );
    System.out.println( "count b in a: " + countCharacter("a", 'b') );

    System.out.println( "count x in empty string: " + countCharacter("", 'x') );
}