You are on page 1of 3

CSC 120 - Strings Page 1 of 3

Exploring Strings

The String Class
A String object stores a sequence of characters as a string. The first character is at position 0, the
second at position 1, etc. A String object also has a length associated with it which is the number
of characters, not the position of the last character (ie. the String January has a length of 7 in
positions 0 through 6). There are many methods defined for gathering information about the
string, comparing strings, searching strings and extracting substrings.

Declaring a String object:

String aString = new String(stringLiteral);

dataType(className) referenceVariable create object using the
parameterized constructor

stringLiteral is any sequence of characters enclosed in double quotes ( ) and is the value
stored in the new String object referenced by aString.

Ex. String month = new String(January);

The following is a partial list of methods defined in the String class:
Each method is illustrated using the variables defined below
String phoneNumber = new String(516-481-7591);
String str1 = new String(Hello);
String str2 = new String (Help);
char c;
int result, position, len;
boolean areTheyEqual;

length
Method header:
public int length( )
Method call:
len = str1.length( );
When this call is executed, the method counts the number of characters stored in the string
referenced by str1 and returns the value 5 which is stored in the variable len.

indexOf
Method header:
public int indexOf(String anotherString)
Method call:
position = phoneNumber.indexOf(481);
When this call is executed, the method determines the position of the first occurrence of the
string 481 and stores the value 4 in the variable position.
CSC 120 - Strings Page 2 of 3
OR
Method call:
position = phoneNumber.indexOf(-);
When this call is executed, the method determines the position of the first occurrence of the
string - and stores the value 3 in the variable position.
If the string is not found, the method returns -1.

charAt
Method header:
public char charAt (int index)
Method call:
c = str1.charAt(0);
When this call is executed, the method returns the character stored at index 0 of the string
referenced by str1 which would be H and stores it in the variable c.

substring
Method header:
public String substring(int beginIndex)
Method call:
str1 = phoneNumber.substring(8);
When this call is executed, the method creates a new string that begins at position 8 and includes
all characters to the end of the string referenced by phoneNumber. The new string, 7591 is
returned and the reference to this string is stored in str1.

Method header:
public String substring(int beginIndex, int endIndex)
Method call:
str1 = phoneNumber.substring(0, position);
When this call is executed, the method creates a new string that begins at position 0 and includes
all characters up to, but not including the value stored in the variable position of the string
referenced by phoneNumber. Assuming position still stores the value 3, str1 would reference
the string 516.

compareTo
Method header:
public int compareTo(String anotherString)
Method call:
result = str1.compareTo(str2);
When this call is executed it compares, character by character, the strings referenced by str1 and
str2. This method returns:
o 0 if the string referenced by str1 and the string referenced by str2 are equal
o a value <0 if the string referenced by str1 < the string referenced by str2
o a value >0 if the string referenced by str1 > the string referenced by str2
The variable result would store a value < 0 since Hello is < Help.


CSC 120 - Strings Page 3 of 3
equals
Method header:
public boolean equals(String anotherString)
Method call:
areTheyEqual = str1.equals(str2);
When this call is executed it compares, character by character, the strings referenced by str1 and
str2. This method returns false if the string referenced by str1 is not equal to the string
referenced by str2 and stores false in the variable areTheyEqual.

Comparing Strings
! to determine whether or not two String objects have the same strings stored in them, you
must use the equals method (defined above)
! you can also use the compareTo method (defined above) to compare the strings stored in two
String objects

Concatenation of Strings
! Strings can be concatenated using the + operator (as you do in System.out.println statements)

You might also like