3. String Methods

3.1. Objectives

  • Utilize popular methods for the String class

3.2. Time Goal

  • 25 minutes on this section
immutable
This is used to describe an object whose value cannot be changed.

3.3. Common Mistakes

  • Thinking that substrings include the character at the last index (they don’t).
  • Thinking that strings can change when they can’t. They are immutable.
  • Trying to invoke a method like indexOf on a string reference that is null. You will get a null pointer exception.

3.4. Exercises

Note

A string holds characters in a sequence. Each character is at a position or index which starts with 0 as shown below. An index is a number associated with a position in a string. The length of a string is the number of characters in it including any spaces or special characters. The string below has a length of 14.

labs/strings/figures/stringIndicies.png:width:500px:align:center:alt:astringwiththeposition(index)shownaboveeachcharacter:figclass:align-center

Figure 1: A string with the position (index) shown above each character

The first character in a string is at index 0 and the last characters is at the index length - 1.

Note

Common String methods

  • str1.compareTo(str2) - returns 0 if they two strings have the same characters, a negative number if str1 is less than str2 and a positive number otherwise.
  • str1.length() - returns the number of characters in a string object including any spaces or special characters.
  • str1.equals(str2) - this method of the String class will return true if the characters in the two strings are the same.
  • str1.indexOf(str2) - returns the index where str2 first starts in str1 or -1 if str2 isn’t in str1.
  • str1.substring(start,end) - returns a new string with all the characters in str1 from start to end - 1.
  • str1.toLowerCase() - returns a new string with the same characters as in str1, but all lowercase.
  • str1.toUpperCase() - returns a new string with the same characters as in str1, but all uppercase.
  • toString(obj) - this is a method that all classes inherit from the Object class. It can be overriden to print out a string representation of an object.

Note

Run the code below to see the output from length, substring, and indexOf.

Note

Did you notice that message1.substring(0,3) includes all the characters from position 0 to 2 and doesn’t include the character at position 3?

    Q-253: What is the value of pos after the following code executes?

    String s1 = "abccba";
    int pos = s1.indexOf("b");
    
  • 2
  • The first character is at index 0 in a string.
  • 1
  • The method indexOf returns the first position of the passed str in the current string starting from the left (from 0).
  • 4
  • Does indexOf start from the left or right?
  • -1
  • Does the string contain a b?

    Q-254: What is the value of len after the following code executes?

    String s1 = "baby";
    int len = s1.length();
    
  • 2
  • Length returns the number of characters in the string, not the number of characters in the name of the string.
  • 3
  • The position of the last character is 3, but the length is 4.
  • 4
  • Length returns the number of characters in the string.
  • -1
  • Length is never negative.

    Q-255: What is the value of str2 after the following code executes?

    String s1 = "baby";
    String s2 = s1.substring(0,3);
    
  • baby
  • This would be true if substring returned all the characters from the first index to the last inclusive, but it does not include the character at the last index.
  • b
  • This would be true if it was s1.substring(0,1)
  • ba
  • This would be true if it was s1.substring(0,2)
  • bab
  • Substring returns all the characters from the starting index to the last index - 1.

    Q-256: What is the value of len after the following executes?

    String s1 = "Miss you!";
    int len = s1.length();
    
  • 7
  • Count spaces and punctuation in the length.
  • 8
  • Did you forget to count a space or punctuation?
  • 9
  • The length method returns the number of characters including spaces and punctuation.

    Q-257: What is the value of str2 after the following code executes?

    String s1 = "baby";
    String s2 = s1.substring(2);
    
  • by
  • The method substring(index) will return all characters starting the index to the end of the string.
  • aby
  • This would be true if it was substring(1);
  • a
  • This would be true if it was substring(1,2);
  • b
  • This would be true if it was substring(2,3);
  • ba
  • This would be ture if it was substring(0,2);

Run the example below to see the output from compareTo and equals.

Note

Strings are immutable which means that they can’t change. Anything that you do to modify a string (like creating a substring or appending strings) returns a new string.

    Q-258: What is the value of s2 after the following code executes?

    String s1 = new String("hi there");
    int pos = s1.indexOf("e");
    String s2 = s1.substring(0,pos);
    
  • hi th
  • The substring method returns the string starting at the first index and not including the last index. The method indexOf returns the index of the first place the string occurs.
  • hi the
  • This would be correct if substring returned all characters between the first index and last index, but does it?
  • hi ther
  • This would be correct if indexOf returned the last position the string str was found in the current string, does it?
  • hi there
  • This would be correct if indexOf returned the last position the string str was found in the current string and if substring included all characters between the start and end index. Check both of these.

    Q-259: What is the value of s1 after the following code executes?

    String s1 = "Hi";
    String s2 = s1.substring(0,1);
    String s3 = s2.toLowerCase();
    
  • Hi
  • Strings are immutable, meaning they don't change. Any method that changes a string returns a new string. So s1 never changes.
  • hi
  • This would be true if the question was what is the value of s2 and it was substring(0,2) not (0,1)
  • H
  • This would be true if the question was what is the value of s2, not s1.
  • h
  • This would be true if the question was what is the value of s3, not s1.

    Q-260: What is the value of s3 after the following code executes?

    String s1 = "Hi";
    String s2 = s1.substring(0,1);
    String s3 = s2.toLowerCase();
    
  • Hi
  • Is this the value of s3? What does toLowerCase do?
  • hi
  • How does substring work? Does it include the character at the end index?
  • H
  • What does toLowerCase do?
  • h
  • s2 is set to just "H" and s3 is set to changing all characters in s2 to lower case.

    Q-261: What is the value of answer after the following code executes?

    String s1 = "Hi";
    String s2 = "Bye";
    int answer = s1.compareTo(s2);
    
  • positive (> 0)
  • H is after B in the alphabet so s1 is greater than s2.
  • 0
  • The method compareTo will only return 0 if the strings have the same characters in the same order.
  • negative (< 0)
  • This would be true if it was s2.compareTo(s1)

    Q-262: What is the value of len after the following executes?

    String s1 = "Hey, buddy!";
    int len = s1.length();
    
  • 8
  • Be sure to count spaces and punctuation in the length (the number of characters in the string).
  • 10
  • Did you forget to count a space or punctuation?
  • 11
  • The length method returns the number of characters in the string, including spaces and punctuation.

    Q-263: What is the value of pos after the following code executes?

    String s1 = "ac ded ca";
    int pos = s1.indexOf("d");
    
  • 3
  • The method indexOf returns the first position of the passed str in the current string starting from the left (from 0).
  • 4
  • The first character is at index 0 in a string, not 1.
  • 5
  • Does the indexOf method find the first occurrence of the character, or the last?
  • -1
  • Does the string contain a d? The pos method will return the first index that the character is at in the string.

    Q-264: What is the value of s1 after the following code executes?

    String s1 = "Hey";
    String s2 = s1.substring(0,1);
    String s3 = s2.toLowerCase();
    
  • Hey
  • Strings are immutable, meaning they don't change. Any method that that changes a string returns a new string. So s1 never changes unless you set it to a different string.
  • he
  • The substring method returns a new string starting at the first index and ending before the second index.
  • H
  • This would be true if we asked what the value of s2 was after the code executes. What is the value of s1?
  • h
  • This would be true if we asked what the value of s3 was after the code executes. What is the value of s1?

    Q-265: What is output from the following code?

    String s = "Georgia Tech";
    String s1 = s.substring(0,7);
    String s2 = s1.substring(2);
    String s3 = s2.substring(0,3);
    System.out.println(s3);
    
  • org
  • The method substring(a,b) means start at a and stop before b. The method substring(a) means start at a and go to the end of the string. The first character in a string is at index 0.
  • eor
  • This can't be true since the e is at index 1 and s2 = s1.substring(2) will start at index 2 and take all characters till the end of the string.
  • eorg
  • This can't be true since the e is at index 1 and s2 = s1.substring(2) will start at index 2 and take all characters till the end of the string.
  • orgi
  • This would be true if substring(a,b) included the character at index b, but it doesn't.
  • You will get an index out of bounds exception
  • This would be true if the starting index was invalid or the ending index was past 2 past the last valid index.

    Q-266: Given the following code segment, what is the value of s1 after the code executes?

    String s1 = "Hi There";
    String s2 = s1;
    String s3 = s2;
    String s4 = s1;
    s2 = s2.toLowerCase();
    s3 = s3.toUpperCase();
    s4 = null;
    
  • null
  • This would be true if we had s1 = s4 after s4 = null was executed. Strings are immutable and so any changes to a string returns a new string.
  • hi there
  • This would only be correct if we had s1 = s2 after s2.toLowerCaase() was executed. Strings are immutable and so any change to a string returns a new string.
  • HI THERE
  • This would be correct if we had s1 = s3 after s3.toUpperCase() was executed. String are immutable and so any change to a string returns a new string.
  • Hi There
  • Strings are immutable meaning that any changes to a string creates and returns a new string, so the string referred to by s1 does not change.
  • hI tHERE
  • Strings are immutable and so any changes to a string returns a new string.
    Drag the definition from the left and drop it on the correct method on the right. Click the "Check Me" button to see if you are correct. Review the summaries above.
  • Returns true if the characters in two strings are the same
  • equals
  • Returns the position of one string in another or -1
  • indexOf
  • Returns a number to indicate if one string is less than, equal to, or greater than another
  • compareTo
  • Returns the number of characters in a string
  • length
        Q-268: Q-267: The main method in the following class should print the part of the message starting with the word "nice".  But, the blocks have been mixed up and include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.

public class Test1 { --- public static void main(String[] args) --- { --- String message = "Have a nice day!"; --- int pos = message.indexOf("nice"); --- System.out.println(message.substring(pos)); --- } --- } --- int pos = message.indexof("nice"); #distractor
        Q-270: Q-269: The main method in the following class should print the initials in lowercase letters. But, the blocks have been mixed up and include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.

public class Test1 { --- public static void main(String[] args) --- { --- String first = "Gerald"; String middle = "Foster"; String last= "Jones"; --- String initials = first.substring(0,1) + middle.substring(0,1) + last.substring(0,1); --- String lowerInitials = initials.toLowerCase(); --- System.out.println(lowerInitials); --- } --- } --- System.out.println(initials); #distractor
Next Section - 4. String Concatenation