This appendix contains String methods that may be useful when
implementing the More Advanced Option specifications for the
Programming Project #2.
(You do not have to import the
String
class
because it is part of a special package that is always automatically
imported.)
Selected Methods from the String
Class:
public int length()
Returns the length of this string. Returns: the length of the sequence of characters represented by this objectpublic boolean isEmpty()
Returnstrue
if and only iflength()
is 0. Returns:true
iflength()
is 0,false
otherwisepublic boolean equals(Object anObject)
Compares this string to the specified object. Parameters:anObject
- The object to compare thisString
against Returns:true
if the given object represents aString
equivalent to this string,false
otherwisepublic boolean equalsIgnoreCase(String anotherString)
Compares this String to another String, ignoring case considerations. Parameters:anotherString
- TheString
to compare thisString
against Returns:true
if the argument is notnull
and it represents an equivalentString
ignoring case,false
otherwisepublic boolean contains(String str)
Returnstrue
if and only if this string contains the specified string. Parameters:str
- theString
to search for Returns: true if this string containsstr
,false
otherwisepublic boolean startsWith(String prefix)
Tests if this string starts with the specified prefix. Parameters:prefix
- the prefix Returns: true ifprefix
is a prefix of this string,false
otherwise. Note also thattrue
will be returned if the argument is an empty string or is equal to thisString
object as determined by theequals(Object)
method.public boolean endsWith(String suffix)
Tests if this string ends with the specified suffix. Parameters:suffix
- the suffix Returns: true ifsuffix
is a suffix of this string,false
otherwise. Note that the result will betrue
if the argument is an empty string or is equal to thisString
object as determined by theequals(Object)
method.public String toLowerCase()
Converts all of the characters in thisString
to lower case. Returns: theString
, converted to lowercasepublic String toUpperCase()
Converts all of the characters in thisString
to upper case. Returns: theString
, converted to uppercasepublic String concat(String str)
Returns a new string representing this string followed by the specified string. This string is unchanged. Parameters:str
- theString
that is concatenated to the end of thisString
Returns: the combined, concatenated string