MathBits.com
Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use  


Strings

The String Class is provided by Java which allows for the discussion and use of strings.  In Java, strings are objects, not primitive types such as int, double, char, and boolean.  Strings are often treated, however, as it they are primitives. 

The term string literal refers to any alphanumeric (character, digit, or both) enclosed in double quotation marks (such as "Snoopy" or "123 West Third Street"). 

Warning!  If a string literal contains only numeric digits ("1234"), it is NOT a number (an integer).  Be careful!  It is just a string of numeric digits that cannot be used to perform mathematical calculations such as addition or multiplication.  Remember that you can perform calculations on numeric data only, not on string literals.                   "1234"   is not the same as  1234
                                           "1234" is a string
                                            1234 is a number

Declaring a String variable is different from declaring an int, double, char or boolean.  The variable name of a String holds the address of the memory location where the actual body of the string is stored. 

Each time a new assignment is stored in the String variable name, the computer finds a new memory location to hold the String text and adjusts the contents of name to hold the address of this new location.  The old memory location is set free.

Variable used in the manner (such as name) are called references.  The address values stored in these variables are said to "point" to the actual data.

Examples of declaring and initializing strings:
String greeting;      // declare the string
greeting = "Hello";   //initialize the string

String title = "Captain";   //declare and initialize at one time
System.out.println (title);  //print 

String poem = Console.readLine("Enter the string");
// This code will read all the characters from the typed "line" up to the
// carriage return and store it in the variable called poem.


 

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:
     int citylength = city.length( );     //stores the length of a city name into an int 
     System.out.println(citylength);   //prints the length

     

  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() );

     
  5. 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"


     

  6. 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

     

  7. 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.);

     
  8. 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"); // returns is a positive
    answer = subject. compareTo("philosophy"); // answer is negative
    answer = subject.compareTo (" mathematics"); //answer is zero

 

                                          


Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use