10.8 Strings in Java


10.8 Strings in Java

In Java, strings are defined as objects of class String, which is a library class. A string variable is really an object reference to the string. A string variable can be declared with a string value:

       String ss = "Kennesaw State"; 

This statement automatically creates an object string referenced by variable ss. A string variable can be declared to reference a string that is the concatenation of two other strings. The concatenation operator + is used to join strings. For example, string variable sfn is a string resulting from joining strings in variables ss and st.

       String st = "University";       String sfn = ss + st; 

10.8.1 Basic Methods in Class String

To get the length of a string, method length() in class String is used. For example, to get the length of a string referenced by variable ss and assign this value to integer variable intlen_ss, the Java statement is:

       int intlen;       ...       intlen_ss = ss.length(); 

The character located at a given index position in a string can be found by invoking method charAt(...) of the string variable. For example, to get the character at index position 5 in the string referenced by variable ss and assign this to character variable mychar:

      char mychar;      ...      mychar = ss.charAt(5); 

Other similar methods defined in class String are:

  • indexOf(...) gets the index position of the first occurrence in the string object of the character in the argument. For example, int intvar = ss.inexOf(mychar); tries to find the first occurrence of character variable mychar in string object ss, and assigns the value to integer variable intvar. If the character cannot be found, this method returns - 1. This method can also be invoked with a substring in the argument; it gets the index position of the first character in the matching substring argument.

  • substring(...) gets the substring in the string object that starts at the given index position. For example, String mysubstr = ss.substring(8); gets the substring that starts at index position 8 in the string referenced by ss and the substring takes the object reference mysubstr. When two arguments are used with this method, the substring has the first argument as the starting index position, and the second argument as the end index position.

  • toLowerCase() gets a copy of the string in lowercase. For example, String mylcstr = ss.toLowerCase(); converts a copy of the string referenced by ss to lowercase and this new string is referenced by mylcstr.

  • toUpperCase() gets a copy of the string in uppercase. For example, String mylcstr = ss.toUppperCase(); converts a copy of the string referenced by ss to uppercase and this new string is referenced by mylcstr.

10.8.2 Comparing Strings in Java

To compare strings, various functions defined in class String can be used. A string s1 can be compared with another string s2 using the equals function in class String. The expression s1 == s2 does not really compare the two strings. It compares if the two variables are referencing the same string object. The equals function of the first string is invoked with the second string as the argument. This function compares two strings and evaluates to a truth-value, so it can be used with an if statement.

For example, the following Java statement tests if the string referenced by s1 is equal to the string referenced by s2:

       String s1;       String s2;        . . .       if (s1.equals(s2))               . . . 

This string comparison only tests if two strings are equal; it is useful in carrying out linear searches. For a binary search of strings, these need to be in alphabetical order. For this search, the comparison needed is more complete in the sense that if the strings are not equal, the order of the first string with respect to the second string needs to be known.

Function compareTo defined in class String compares two strings and evaluates to an integer value. If this integer value is equal, the two strings are equal. If the integer value is greater than zero, the first string is higher alphabetically with respect to the second string. If the integer value is less than zero, the first string is lower alphabetically with respect to the second string. The compareTo function of the first string is invoked with the second string as the argument. This function is invoked in an assignment statement. For example, the comparison of strings s1 and s2 is:

       String s1;       String s2;       int test;  // result of string comparison       . . .       test = s1.compareTo(s2);       if (test == 0)          . . .       if (test > 0)          . . .       else          . . . 

In the example, variable test holds the integer values with the result of the string comparison of s1 and s2. Variable test is subsequently evaluated in an if statement for further processing.

Caution

Because string objects cannot be modified, Java provides class StringBuffer. This library class can be instantiated by creating string objects that can be modified.

Several methods of class StringBuffer are similar to the ones in class String. Other methods, such as insert(...), allow a character or a substring to be inserted at a specified index position.




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net