|
|
||
| You are here: | ||
|
Although students are required to know these methods they do not need to memorize them for examinations. Any examination question requiring their use will include a reminder as to purpose and syntax.
|
Students are required to know the following methods of the String class:
They are also expected to know that the + operator concatenates Strings (joins them together) and forces other types (casts them) to String automatically, for example: int answer = 6; The equals method is used to compare two Strings (do not use the == operator with Strings): String name = "fred" The substring (int, int) method can be used to extract a String from another String, for example: String name = "Fred Smith"; Notice that counting begins at 0 - the first character. This method can throw a StringIndexOutOfBounds Exception (a runtime error) if not caught - see exception handling ). An alternate substring(int) method of this class takes the end of the String starting at the index given in the parameter, eg: String name = "Fred Smith"; This second version is not part of JETS, however. The length () method returns the number of characters in the String (including any spaces). It is often useful when testing if the user has input anything at all into a TextField, for example: TextField name = new TextField("Please input your name"); The indexOf (String) method is useful for searching or parsing Strings. It returns the position of the String parameter within the target String (or -1 if the String does not occur in the target). It returns the first position if there is more than 1 occurrence. The first index in a String is 0. String stuff = "Hello there, Yoshitaka. Nice day for a walk in the park."; Other useful methods of the String class .split() An array needs to be declared firs, then components of the String can be split up, either by default at spaces or at any other character if desired. An example fo String.split() can be found on this page. .trim() Removes any leading or trailing spaces from a string. .lastIndexOf Like indexOf but looks for the last occurrence. |
add title! |
|
|
|||
|
Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand; This page was last modified: May 31, 2009 |