Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

Some useful String methods

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;
   display.append("" + answer);

The equals method is used to compare two Strings (do not use the == operator with Strings):

   String name = "fred"
   if (name.equals("Fred")
// false in this example
   {
     
// result if true
   }

Back to top

The substring (int, int) method can be used to extract a String from another String, for example:

 String name = "Fred Smith";
  String first = name.substring(0,4); // first now has the value "Fred"

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";
  String last = name.substring(4); // last now has the value " Smith"

This second version is not part of JETS, however.

Back to top

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");
   if (name.length() != 0)
   {
     
 // process name
   }
   else
   {
      
// error, nothing has been input
   }

Back to top

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.";
  int pos = stuff.indexOf("Hello");  // returns 0
  int pas = stuff.indexOf("hello");  // returns -1
  int prs = stuff.indexOf("ark.");   // returns 52

Back to top

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!


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

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.

Creative Commons License


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