<Previous    Back to Start  Contents   Next>


1.2 Exam Only: Miscellaneous


Write code to demonstrate the use of the following methods of the java.lang.String class: length(), toUpperCase(), toLowerCase(), equals(), equalsIgnoreCase(), charAt(), concat(), indexOf(), lastIndexOf(), substring(), toString(), trim().


length() - Returns the length of this string.

toUpperCase() - Converts this string to uppercase.

toLowerCase() - Converts this String to lowercase.

[Note that toLowerCase() and toUpperCase() will return a reference to original string object if the it is already in lower case or upper case respectively. This is important for questions on the == operator and strings. There appears to be a bug in Suns JDK 1.1 which causes this not to occur, but this is the behaviour described by the JavaDocs. Java2 behaves as described in the JavaDocs.]

equals(Object) - Compares this string to the specified object. Returns true if the strings contain the same sequence of characters.

equalsIgnoreCase(String) - Compares this String to another object. Returns true if the strings contain the same sequence of characters, ignoring case.

charAt(int) - Returns the character at the specified index. An index ranges from 0 to length() - 1.

concat(String) - Concatenates the specified string to the end of this string.

indexOf(int) - Returns the index within this string of the first occurrence of the specified character.

indexOf(int ch, int fromIndex) - Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
Parameters:

ch - a character.
fromIndex - the index to start the search from.

indexOf(String) - Returns the index within this string of the first occurrence of the specified substring.

indexOf(String, int) - Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

lastIndexOf(int) - Returns the index within this string of the last occurrence of the specified character.

lastIndexOf(int ch, int fromIndex) - Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.
Parameters:

ch - a character.
fromIndex - the index to start the search from.

lastIndexOf(String) - Returns the index within this string of the rightmost occurrence of the specified substring.

lastIndexOf(String, int) - Returns the index within this string of the last occurrence of the specified substring.

substring(int) - Returns a new string that is a substring of this string. The substring begins at the specified index and extends to the end of this string.

substring(int beginIndex, int endIndex) - Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1.

Remember: substring gives you the substring from the first index, to the second index minus one.

trim() - Removes white space from both ends of this string.

toString() - Returns a string representation of the object. This method is inherited by all classes from Object. By default, the toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character '@', and the unsigned hexadecimal representation of the hash code of the object. You can override it in your own classes to produce some useful string e.g. for debugging info. For the Strings, toString() returns the String object itself.


State all operators that are legal in String expressions.

The + operator to concatenate strings.


<Previous    Back to Start  Contents   Next>

©1999, 2000, 2002 Dylan Walsh.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being the disclaimer, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".