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.
    Example:
     String city = "Fulton"; 
     int citylength = city.length();    
    //stores the length of city into an int 
     System.out.println(citylength);  
    //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.
    Example:

    String holiday="Thanksgiving";
    System.out.println(holiday.charAt(4));    
    //prints out a 'k'

    Example:
    String puppy ="Wally";
    System.out.println(puppy.charAt(0));   
     //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.
    Example:

    String burp = "          hic up          ";
    System.out.println(burp.trim());  
    // prints out hic up

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

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

    beware
    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.
    Example:
    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.
    Example:

    String sport = "football";
    System.out.println(sport.substring(1));  
    // prints out ootball

  7. indexOf("Garfield") Returns the beginning subscript of the string where "Garfield" is found
    Example:
      (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.
    Example:
      (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.
    Example:

    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

 


divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com