Difference between revisions of "String Methods"

From CompSciWiki
Jump to: navigation, search
(equals())
(equals())
Line 3: Line 3:
 
==String Methods==
 
==String Methods==
 
===equals()===
 
===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:
+
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:
  
 
<pre>
 
<pre>

Revision as of 13:41, 21 March 2007

COMP 1010 Home > Calling Methods


Introduction

There are only two String methods that you need and they are both very similar. After this chapter you will be able to check if two strings of text are the same.

   

{{{Body}}}

String Methods

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.