Difference between revisions of "String Methods"

From CompSciWiki
Jump to: navigation, search
(String Methods)
(String Methods)
Line 2: Line 2:
  
 
==String Methods==
 
==String Methods==
In the beginning of Java programming learning process, you have learned that the equal sign ''='' determines the primitive types (eg. int, double, char) equality. However, for Strings, other built-in String methods are used to find the equality. String is just another toolbox and its methods are the tools to simplify work related to Strings.
+
In the beginning of Java programming learning process, you have learned that the equal sign '''=''' determines the primitive types (eg. int, double, char) equality. However, for Strings, other built-in String methods are used to find the equality. String is just another toolbox and its methods are the tools to simplify work related to Strings.
 
===equals()===
 
===equals()===
 
This method compares two strings and returns a boolean result <font color="red">(true or false)</font>. Think of it as a method that is applied to a string to see if it equals another. Example:
 
This method compares two strings and returns a boolean result <font color="red">(true or false)</font>. Think of it as a method that is applied to a string to see if it equals another. Example:

Revision as of 14:03, 21 March 2007

COMP 1010 Home > Calling Methods


Introduction

There are three useful String methods that you mostly need. Two of them are very similar (comparing the equality of two Strings); and the other is used to find the length of the string you are working with.

   

{{{Body}}}

String Methods

In the beginning of Java programming learning process, you have learned that the equal sign = determines the primitive types (eg. int, double, char) equality. However, for Strings, other built-in String methods are used to find the equality. 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). Think of it as a method that is applied to a string to see if it equals another. Example:

String testString;
testString = "I am test";

if testString.equals("I am test")
{
     //testString equals "I am test"
}
else
{
     //testString does not equal "I am test"
}

The above example will result in the if statement being true. It is important to 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".

For a full list of all the String methods you can visit http://java.sun.com/javase/6/docs/api/java/lang/String.html and scroll down to the Method Summary table.

length()