String Class and Methods

In Unit 2 we started working with String variables and some of the methods associated with Strings. Let's take a closer look at the methods in the String class.

String is located in the java.lang package.
The full class name is java.lang.String.
Remember, NO importing is needed to utilize the java.lang package.

Sample ways to engage these new methods:

System.out.println(name.length()); //prints the string length

char letter = name.charAt(4); //stores 1 letter from String as char

Unlike the Character class and the Math class, we do not write the word "String" preceding a call to these methods. Instead we use a String object (such as name or city) that has already been declared as a String in the program.

String Methods:
String is a "class" and as such has methods that are properties of the class.  To use a method, we need the dot operator.

  1. length( )  This is a numerical returning method that gives the length of the string.  It returns an integer value.
     String city = "Fulton"; 
     int citylength = city.length();    
    //stores the length of city into an int 
    //prints the length of the string which is 6


  2. charAt(n)  This is a value returning method. The argument states which character to return. The subscripting of the locations of the characters starts with zero.

    String holiday="Thanksgiving";
    //prints out a 'k'

    String puppy ="Wally";
     //prints out a 'W'

  3. trim( ) This is a value returning method that returns a copy of the argument after removing its leading and trailing blanks (not embedded blanks - blanks within the statement).  The string will not be changed in memory.

    String burp = "          hic up          ";
    // prints out hic up

  4. toLowerCase( )  In memory, the string is not changed, but it will be printed out in all lower case letters.
    String cityname = "Houston";
    // prints out houston

  5. toUpperCase( ) In memory, the string is not changed, but it will be printed out in all upper case letters.
    String cityname = "Houston";
    //prints out HOUSTON

    The methods toLowerCase and toUpperCase listed in these methods for Strings are not the same as the ones listed under the methods for Characters. The methods on this page deal with String variables, where as the Character section deals with char variables.

  6. substring(Start, End)  This is a value returning method.  A string is returned  beginning at Start subscript up to but not including the End subscript.
    String sport = "football";
    System.out.println(sport.substring (1,3));  
    // prints out oo

    substring(Start)  This is an alternate version.  It returns the substring beginning at Start subscript up to and including the end of the string.

    String sport = "football";
    // prints out ootball

  7. indexOf("Garfield") Returns the beginning subscript of the string where "Garfield" is found
      (find the comma in a city, state zip listing)
    String city = "Fulton, New York 13069";
    System.out.println (city.indexOf (","));  
    // prints 6

  8. equals( )   Used to compare two strings with the method to see if they are exactly the same, this includes any blanks or spaces within the string.
      (check to see if the user entered a dog name of Snoopy)
    if (dogname.equals("Snoopy"))
    System.out.println("The user entered Snoopy.");

  9. compareTo( )  Compares strings to determine alphabetic location.  Returns a zero if the two strings are equal, a negative if the first string is alphabetically before the compared string, and a positive if the first string is alphabetically after the compared string.

    String subject = "mathematics";
    boolean answer;
    answer = subject.compareTo ("biology");
     // answer is positive - math comes after biology
    answer = subject.compareTo("philosophy");
    //answer is negative - math comes before philosophy
    answer = subject.compareTo ("mathematics");
     //answer is zero - math is math


Return to Unit Menu | | | Terms of Use  |