String Methods

From CompSciWiki
Revision as of 19:25, 6 December 2011 by CharuC (Talk | contribs)

Jump to: navigation, search

COMP 1010 Home > Calling Methods


Introduction

You will learn how to use simple API String Methods such as comparing Strings and finding the length of a String.

String Methods

In the beginning of the Java programming learning process, you have learned that the equal sign "==" determines equality of primitive types (eg. int, double, char). However, other built-in String methods are used to find the equality of two Strings. String is just another toolbox and its methods are the tools to simplify work related to Strings.

equals

This method compares two Strings and returns a boolean result (true or false). True, if they are the same string and false, if they differ in any way. Think of it as a method that is applied to a String to see if it equals another. This method is required for Strings because the '=' symbol is used for assignment purposes. Example:

Code

 String testString;
testString = "I am test";

if( testString.equals("I am test") )
{
     //fill in with code you wish to execute when testString equals "I am test"
}
else
{
     //fill in with code you wish to execute when testString does not equal "I am test"
} 

The above example will result in the if statement being true. Note that equals() must be applied to a string variable.

equalsIgnoreCase

This method is the same as the one above but the result will be true even if the cases are different. "I AM TEST" would be the same as "i am test".

indexOf

This method finds the location of a specific character or String inside a given String. For example:

Code

 final String NAME = "Henry VIII" ;
System.out.println("The first name is " + NAME.indexOf(" ") + " characters long") ;
System.out.println("The first name is " + NAME.indexOf(' ') + " characters long") ; 

The value returned from indexOf is the number of characters to the left of the first occurrence of the argument. The argument can be either a String or a character (both are shown above). If the argument does not appear in the String at all, indexOf returns a value of -1. This method is often used with substring (see below) to extract part of a String.

length

This method simply returns the number of characters (including spaces, tabs, etc.) in the String that calls it. For example:

Code

 String test = "I am test";
int length = test.length();

System.out.println("The string's length is: " + length); 

Which gives the result:

The string's length is: 9

substring

This method is used to extract part of the given String. For example, to separate an incoming name into first and last names, you could use the following code:

Code

 final String NAME = JOptionPane.showInputDialog("Enter your name") ;
final int BLANK_LOC = NAME.indexOf(' ') ;
final String FIRST_NAME = NAME.substring(0,BLANK_LOC) ;
final String LAST_NAME = NAME.substring(BLANK_LOC+1) ; 

This method can have one or two integer arguments. If it has one argument, say 5, substring discards 5 characters from the front of the String and returns what's left. If it has two arguments, 3 and 5, it discards 3 characters from the front, and then returns what's left up to position 5. The number of characters returned is the difference between the two arguments. For example,

"Paraguay".substring(2,4) gives  "ra"

More Methods

For a full list of all the String methods you can visit The API String Methods and scroll down to the Method Summary table.

Previous Page: Calling Methods Next Page: Math Methods