Difference between revisions of "String Methods"

From CompSciWiki
Jump to: navigation, search
(equals())
Line 1: Line 1:
{{1010Topic|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. |Overview=You will learn how to use simple API String Methods|Chapter_TOC=[[Calling Methods]]}}
+
{{1010Topic|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.|Overview=You will learn how to use simple API String Methods such as comparing strings together and length of String.|Chapter_TOC=[[Calling Methods]]}}
  
 
==String Methods==
 
==String Methods==
Line 25: Line 25:
  
 
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.
 
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()===

Revision as of 13:50, 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

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()