String Methods

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Calling Methods


Introduction

You will learn how to use simple String Methods such as comparing Strings and finding the length of a String. String is just another toolbox and its methods are the tools to simplify work related to Strings.

String Methods

equals

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, the equals() method is needed to find the equality of two Strings. 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

 public class testStrings
{
	public static void main(String args[])
	{
		String testString;
		testString = "I am test";

		if( testString.equals("I am test") ) 
                //if the value of testString equals "I am test", then print the following code
		{
		     System.out.println("String equals I am test");
		}
		else                                 
		//if not print the following
                {
			System.out.println("String equals I am test");	
		}
	}
} 

Ouptut

 String equals 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 equals method 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. In the following example, indexOf is used first to find the location of the String " ", and then the character ' '. Note the different use of the " " and the ' ' to represent the String space and the char space. Both are valid.


Code

 public class indexOf
{
	public static void main(String args[])
	{
	         String NAME = "Henry VIII" ;

                // Use String method indexOf to find the location of the String " "
		System.out.println("The first name is " + NAME.indexOf(" ") + " characters long") ;

                // Use String method indexOf to find the locate of the Char ' '
		System.out.println("The first name is " + NAME.indexOf(' ') + " characters long") ;
	}
} 

Output

 The first name is 5 characters long
The first name is 5 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

 public class Length
{
	public static void main(String args[])
	{
		String test = "I am test";
		int length = test.length();			
		System.out.println("The string's length is: " + length);
	}
} 

Output

 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

 import javax.swing.JOptionPane;
public class SubString
{
	public static void main(String args[])
	{
		//Input the name using a GUI dialog with the help of JOptionPane
		final String NAME = JOptionPane.showInputDialog("Enter your name") ;

		//Find the location of a space in the string that has been input
		//so that you can divide the name into first and last name
		final int BLANK_LOC = NAME.indexOf(' ') ;

		//get the firstname by selecting the first part of the string
		//i.e. till the space appears. The substring parameters allow you to
		//choose the starting location and the ending location of the substring
		final String FIRST_NAME = NAME.substring(0,BLANK_LOC) ;

		//similar to the firstname, select the lastname by selecting the part
		//from space till end of string. Here in the substring parameters we are only
		//providing the begining part of the string. it automatically goes till
		//the end of the string.
		final String LAST_NAME = NAME.substring(BLANK_LOC+1) ;

		System.out.println("The First name is "+FIRST_NAME + "\nThe Last name is " + LAST_NAME);
	}
} 

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.

Output

If the code is run and the user enters Charu Chandra in the JOptionPane window, then the following output would be produced.

 The First name is Charu
The Last name is Chandra 

More Methods

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

Summary

In this section, you have learnt how to make use of various string methods to make operations with strings easier. Particularly, we learnt to use the string methods equals, length, substring and indexOf.

Previous Page: Calling Methods Next Page: Math Methods