Java Reference
In-Depth Information
As with any method, a
String
method is called (invoked) by writing a
String
object, a dot, the name of the method, and finally a pair of parentheses that enclose any
arguments to the method. Let's look at some examples.
As we've already noted, the method
length
can be used to find out the number
of characters in a string. You can use a call to the method
length
anywhere that
you can use a value of type
int
. For example, all of the following are legal Java
statements:
length
String greeting = "Hello";
int
count = greeting.length();
System.out.println("Length is " + greeting.length());
Display 1.4
Some Methods in the Class
String
(part 1 of 4)
int
length()
Returns the length of the calling object (which is a string) as a value of type
int
.
EXAMPLE
After program executes
String greeting = "Hello!";
greeting.length()
returns
6
.
boolean
equals
(
Other_String
)
Returns
true
if the calling object string and the
Other_String
are equal. Otherwise, returns
false
.
EXAMPLE
After program executes
String greeting = "Hello";
greeting.equals("Hello")
returns
true
greeting.equals("Good-Bye")
returns
false
greeting.equals("hello")
returns
false
Note that case matters.
"Hello"
and
"hello"
are not equal because one starts with an
uppercase letter and the other starts with a lowercase letter.
boolean
equalsIgnoreCase
(
Other_String
)
Returns
true
if the calling object string and the
Other_String
are equal, considering upper- and
lowercase versions of a letter to be the same. Otherwise, returns
false
.
EXAMPLE
After program executes
String name = "mary!";
greeting.equalsIgnoreCase("Mary!")
returns
true