String Methods

The methods you need to know for the AP Exam are:

There are a few additional ones that are great to know and they are denoted by ***.

class java.lang.String implements java.lang.Comparable

Method Signature
Return Type

int compareTo (Object other)

Note: other can be a String because a String is an Object

Example:

String s1 = "Tom";

String s2 = "Mary";

if (s1.compareTo(s2) > 0)

System.out.println(s1 + " is greater than " +s2);

else if (s1. compareTo(s2) < 0)

System.out.println(s1 + " is less than " +s2);

else if(s1.compareTo(s2) == 0)

System.out.println(s1 + " is equal to " +s2);

returns -1 (or a negative integer) if this is less than other

returns 0 if this is equal to other

returns 1 (or a positive integer) if this is greater than other

 

In the example to the left, s1 invokes compareTo so it is this and s2 is other

boolean equals (Object other)

Note: other can be a String because a String is an Object

Example:

String s1 = "Tom";

String s2 = "Mary";

if (s1.equals(s2) )

System.out.println("The strings are equal.");

returns true if the content of this is equivalent to the content of other

In the example to the left, s1 invokes equals so it is this and s2 is other

int length ()

Example:

String s3 = "Java";

System.out.println(s3 + " has " + s3.length() + " letters.");

Output: Java has 4 letters.

returns the size (number of characters) in the string

String substring (int beginIndex, int endIndex)

Example:

String s4 = "programming";

String s5 = s4.substring(3, 7); // extracts letters at indices 3, 4, 5, & 6 but not 7.

System.out.println(s5);

Output: gram

returns the substring beginning at beginIndex and ending at endIndex-1 (does not include the character at endIndex)

String substring (int beginIndex)

Example:

String s6 = "programming";

String s7 = s6.substring(7); // extracts letters at indices 7, 8, 9, & 10.

System.out.println(s7);

Output: ming

returns substring from beginIndex to length()

int indexOf (String s)

Example:

String s8 = "programming";

int i = s8.indexOf("gram");

System.out.println("gram is located beginning at index " + i);

Output: gram is located beginning at index 3

returns the index (an int) of the first occurrence of the string s inside of this or -1 if not found

String trim ()

Example:

String s9 = "_ _ _ _ _ programming";

String s9 = s9.trim();

Note: its simplest to rereference s9 to the value returned by trim

System.out.println(s9);

Output: programming

***

returns a String value without any leading blank spaces

boolean equalsIgnoreCase (Object other)

Note: other can be a String because a String is an Object

Example:

String s1 = "JaVa";

String s2 = "jAvA";

if (s1.equalsIgnoreCase(s2) )

System.out.println("The strings are equal.");

Output: The strings are equal.

***

returns true if the content of this is equivalent to the content of other no matter what the case of either s1 or s2 is.

In the example to the left, s1 invokes equals so it is this and s2 is other

String toLowerCase ()

Example:

String s = "jAvA";

s = s.toLowerCase();

Note: its simplest to rereference s to the value returned by toLowerCase

System.out.println(s);

Output: java

***

returns a String value with all letters being lower case

String toUpperCase ()

Example:

String s = "jAvA";

s = s.toUpperCase();

Note: its simplest to rereference s to the value returned by toLowerCase

System.out.println(s);

Output: JAVA

***

returns a String value with all letters being upper case

char charAt (index i)

Example:

String s = "KCD";

char ch;

for (int i = 0; i < s.length(); i++)

{

ch = s.charAt(i);

System.out.println(ch);

}

Output:

K

C

D

***

returns a char value from the ith position of the string