The String and StringBuffer Classes

The Java language contains advanced string-handling capabilities built into its String and StringBuffer classes. A String object represents a string that has a constant value that cannot be changed after the string has been created. A StringBuffer object represents a string that can change, including growing in size.

Strings represent constant objects. Most string-related functions expect String values as arguments and return String values.

StringBuffers contain several methods to modify a StringBuffer's contants. If you change a string often during your program, use a StringBuffer.

The String Class

Synopsis

public final class String extends Object



Below are some of the constructors for the String class.
public String (String value)Constructs a new String that is a copy of the specified String.
public String (char value[])
Constructs a new String whose value is the specified array of characters.
public String (char value[], int offset, int count)
Constructs a new String whose initial value is the specified subarray of characters. The length of the new string will be count characters starting at offset within the specified character array.

Trows StringOutOfBoundsException if offset and count are invalid.

As you might imagine, strings can be complex and several useful functions can be performed on strings. Fortunately, most of these useful functions have been coded into the String class.



Below are some of the methods to the String class
public int length () Returns the length of the String (the number of 16-bit Unicode characters in the string).
public char charAt (int index)
Returns the character at the specified index.

String Comparison Methods


public boolean equals (Object obj)Compares this String to the specified object obj. Returnes true if the object has the same length and the same characters in the same sequence as this String.
public boolean equalsIgnoreCase (String s) -Compares this String to another string s. Returnes true if the String s has the same length and the same characters in the same sequence as this String.
public int compareTo (String s)Comparesthis Stringto another String s. Returns an integerthat is less than, equal to, or greater than zero depending on whether this String is less than, equal to, or greater than String s.
public boolean regionMatches (int toffset, String other, int ooffset, int len)Determines whether a region of this String matches the specified region of this specified String other.
  • toffset-Where to start looking in this String
  • other-The other String
  • ooffset- Where to start looking in the other String
  • len-The number of characters to compare
public boolean startsWith (String prefix)Determines whether this String starts with prefix.
public boolean endWith (String suffix)Determines whether this String ends with suufix.
public String substring (int beginIndex, int endIndex)Returnes the substring of this string specified by beginIndex and endIndex.
Throws StringIndexOutOfBoundsExcepton if the beginIndex or the endIndex is out of range.
public String concat (String ctr)Creates a new String object from this String, by concatenating the specified string str to the end of this String.

Conversion Methods


public String toLower ()Creates a new String object, converting all of the characters in this String to lowercase.
public String toUpperCase ()
Creates a new String object, converting all of the characters in this String to uppercase.
public String trim ()
Creates a new String object from this String with leading and trailing white space removed.
public String toString ()
Converts this String to a String, returned the String itself.
public char [] toCharArray ()
Converts this String to a character array, creating a new array.

ValueOf Methods


The String class contains several functions devoted to converting values of other data types into their string representations. These conversion functions all have the name valueOf, but th methods is overloaded to accomodate all the various data types.

Below are some of the valueOf methods in the String class:
public static String valueOf(type x)Returns a String object that represents the value of the specified type x, where type is int, float, long, double, char, boolean, or Object.
public static String valueOf(char data[])
Returns a String that is equivalent to the specified character array.Uses the original array as the body of the String (it does not copy the original array to a new array:return new String(data)).
public static String copyValueOf (char data[])
Returns a String that is equivalent to the specified character array, creating a new array by copying the characters into it.

The code fragment below illustrates some of the methods in the String class:

 String One = new String ("hello world") ;
           float f = 3.141592f;
           // String PI = One.valueOf (f);
           String PI = String.valueOf (f)  // more correct!!

           // convert to upper case
           System.out.println("String One: "+ One.toUpperCase());
           // concatenate two strings
           System.out.println("String One+PI: "+One.concat (PI));

==Operator and equals () Methods


When comparing string, there is a difference between String literals and String objects. Use the equals method to test the equality of String objects and the == operaterto test quality of String literals:


class stringtest {
   public static void main (String args[]) {
       String x = "abc";
       String y = new String ("abc");
       String z = "abc";

       if (x == y)
          System.out.println ("x == y");
       else if (x.equals (y))
          System.out println ("x equals y");

       if (x == z)
          System.out.println ("x == z");
       }
    }

This program produces the following output:

$ java stringtest
x equals y
x == z
$

Back to Java Language