Java Reference
In-Depth Information
Finally, for comparing multidimensional arrays, there is the deepEquals()
method. It returns a boolean and works recursively on nested arrays of any
depth. Two array references are considered deeply equal if they contain the same
number of elements and all corresponding pairs of elements in the two arrays are
deeply equal. (If both array references are null they are also considered deeply
equal.)
10.11 Tools for strings
We briefly discussed the String class in Chapter 2 and in Chapter 3. We
also used strings frequently in the various demonstration programs. Here we
look further at tools for dealing with strings including the many useful meth-
ods in the String class itself and in StringBuffer , StringBuilder , and
StringTokenizer .
10.11.1 String class methods
In Chapter 3 we discussed the valueOf() methods in the String class for
converting primitive type values to strings. The String class contains a large
number of other useful methods. Here we briefly examine a sample of these
methods.
10.11.1.1 int length ()
This method returns the number of characters in the string, as in
String str =" A string " ;
int x = str.length ();
This results in variable x holding the value 8.
10.11.1.2 String trim ()
Removes white space from the leading and trailing edges of the string:
String string =" 14 units " ;
String str = string.trim ();
This results in the variable str referencing the string “14 units .Asalways,
String objects are immutable. The trim() method always returns a new
String object containing the trimmed version of the original String .
10.11.1.3 int indexOf (int ch) , int lastIndexOf (int ch)
The indexOf() method returns the index, starting from 0, for the location of the
given character in the string. (The char value is widened to int .) For example,
String string =" One fine day " ;
int x = string.indexOf ( ' f ' );
Search WWH ::




Custom Search