Monday June 26, 2000




Dealing with Strings


The class String is found in the java.lang package. Recall that classes found in the java.lang package are considered fundamental to the Java programming language.

Because a string is very useful to us there are several predefined methods available.

  • public char charAt(int index)

    This is a method which returns the character located at a certain position in a string.

    One thing that we must be aware of about Java is that it begins counting at 0 and not at 1.

    For example, in the string "Hello", charAt(0) would return the character `H'.

    If a string has n characters, then the last character will be at position n-1.

    Note that since the positions of the characters in a string are identified by integers, a for loop is well-suited to working with charAt.

    For example, suppose we wish to print out each character contained in the string "Hello". We could use the following for loop.

    String s = "Hello";
    
    for (int i=0;i <= 4;i++)
       System.out.println(s.charAt(i));
    
    Note that in this example we knew how many characters were in the string. However, we will not always know this. For this reason, there is a method associated with strings called length.

  • public int length()

    length returns an integer that represents the number of characters found in a string.

    For example, if we read input from the keyboard, we will not always know how many characters the user will input. We can determine this information using length.

    InputStreamReader stream = new InputStreamReader(System.in);
    BufferedReader keyboard = new BufferedReader(stream);
    
    String s = keyboard.readLine();
    
    System.out.println("You entered " + s.length() + " characters.");
    
    Another useful method is endsWith.

  • public boolean endsWith(String suffix)

    This method determines whether or not a certain string ends in a certain character sequence.

    For example, we know that a binary number that ends with a 1 represents an odd decimal number, and a binary number that ends in 0 represents an even number.

    InputStreamReader stream = new InputStreamReader(System.in);
    BufferedReader keyboard = new BufferedReader(stream);
    
    System.out.println("Please enter a binary number");
    
    String s = keyboard.readLine();
    
    if (s.endsWith("1"))
       System.out.println("The decimal equivalent of " + s + " is odd.");
    else
       System.out.println("The decimal equivalent of " + s + " is even.");
    
  • public boolean equals(Object anObject)

    We may use this method to test whether or not two strings are equal. Note the parameter list here calls for something of type Object. But since all classes extend the Object class, all objects are of type Object.

    For example, suppose we have two strings, s1 and s2, and wish to determine whether or not they are the same.

    String s1,s2;
    
    if (s1.equals(s2))
       System.out.println("s1 and s2 are equal");
    else
       System.out.println("s1 and s2 are not equal");
    
    Another important operations related to strings is the ability to extract part of the string. We do this with the substring method. This is an overloaded method.

  • public String substring(int beginIndex)

    This form of the method returns the portion of the original string beginning at position beginIndex and extending to the end of the string.

  • public String substring(int beginIndex,int endIndex)

    This form of the method returns the portion of the original string from position beginIndex to the position endIndex-1.

    Let s = "Hello".

    s.substring(1) would return the string "ello".

    s.substring(2) would return the string "llo".

    s.substring(0,2) would return the string "He".

    s.substring(1,3) would return the string "el".

    Concatenation is another important operation related to strings. We have already seen that we can use the + symbol to concatenate strings in a System.out.println statement.

    There is also a method in the String class which which allows us to concatenate.

  • public String concat(String str)

    This method allows us to concatenate str to the end of the current string.

    For example, suppose s = "Hello".

    s.concat(" World") would return the string "Hello World".

    Exercises

    These exercises will give you some practice at using some of these methods.

    1. Input a string from the user and print out a string which contains the characters of the original string reversed. That is, if the input string was "Hello", your output string would be "olleH".

    A Solution

    2. Input a string from the user and count the number of times the character `H' appeared in the string and report that to the user. That is, if the input string was "Hello", you would report to the user that the character `H' appeared one time in the string.

    A Solution

    3. Input a string from the user and replace every occurrence of the character `e' with the character `a'. That is, if the input string was "Hello", you would change it to "Hallo".

    A Solution

    4. Input a string from the user and report whether or not that string ends with the character `i'. That is, if the input string was "Hello", then you would report that the string does not end with i.

    A Solution

    5. Input a string from the user and report an error if that string contains anything other than the characters `1' and `0'.

    A Solution