Java Strings: Substrings
Substrings
A substring is a part of a string, and the substring() method allows us to take any part of any string to create another string. We can use the substring() method in two ways:
- With one argument, substring(x), that specifies a start position and includes the remaining characters of the string.
- With two arguments, substring(x, y), that specifies both a start and end position.
Let’s create a new string called schoolString and assign it the value “Learning Glue“:
String schoolString = "Learning Glue";
Now, using the substring method let’s output a few substrings:
- The word Learning
- The word Glue
- The Phrase earning G
How many characters does the string Learning Glue have?
Did you count 12?
If so, remember that when Java counts characters it includes more than just alphanumeric characters (or letters and numbers), it also includes special characters like £, $ or *, and also SPACES.
So let’s count again: Learning Glue, this time you should see we have a 13 character string.
Now that we understand what to include when counting characters in a string, let’s get back to creating the three substrings mentioned above.
First, the word Learning. This word is at the start of the string Learning Glue that is stored in the variable schoolString. In Java, and many other languages, we start counting elements (like characters in a string) from 0 rather than 1. So, the first character, L, in the string Learning is character 0. This may be counter intuitive to us humans but to a computer it makes perfect sense.
Now that we know where we are counting from let’s consider which version of the substring() method we should use; with 1 argument or 2.
Which substring() method do you use when you need to specify a start and end character: substring(x) or substring(x,y)?
Congratulations if you picked substring(x,y). This method requires two arguments, a start and end character.
substring(x) requires only 1 argument, a start character and it assumes the remainder of the string is required.
Next, how do we determine which argument(s), or beginIndex and endIndex, to use? Earlier we saw how the position o index of elements, like characters in a string, are counted from 0, and that the first letter in the substring Learning is at index 0. That is the first argument for the substring() method. The second argument will be the letter g, so counting the characters in Learning from index 0:
- L=0
- e=1
- a=2
- r=3
- n=4
- i=5
- n=6
- g=7
The last character is at index 7, or the endIndex is 7, so you might think substring(0,7) will include all the required characters BUT there is something else you need to know about the substring() method. It treats the beginIndex as inclusive but the endIndex as exclusive. In other words, the the substring starts at beginIndex and ends at endIndex-1.
So what should the arguments for substring(x,y) be?
Well done if you guessed substring(0,8)
Since the beginIndex as inclusive and the endIndex as exclusive we need to add 1 to the endIndex to ensure it is included in the substring.
Let’s try this for real. In your IDE type in the following code:
String schoolString = "Learning Glue"; System.out.println("Our string is: " + schoolString); String subString01 = schoolString.substring(0,8); System.out.println("The 1st substring is: " + subString01);
The output should be:
run: Our string is: Learning Glue The 1st substring is: Learning BUILD SUCCESSFUL (total time: 0 seconds)
Let’s move onto the next substring: the word Glue. This word contains the last 4 characters of our string Learning Glue.
Which substring() method do you use when you need to only specify a start character: substring(x) or substring(x,y)?
Congratulations if you picked substring(x). This method requires only one argument, the index of the start character and it assumes the remainder of the string is required.
substring(x,y) requires 2 arguments, a start and end character.
Now that we know which substring() method to use, what argument should we use? Since there is only 1 argument required, and that is the beginIndex, we need to count the number of characters up to the first character of the required substring:
- L=0
- e=1
- a=2
- r=3
- n=4
- i=5
- n=6
- g=7
- =8
- G=9
So, the beginIndex is 9, therefore the substring() method will be: substring(9). Let’s add this to our program:
String schoolString = "Learning Glue"; System.out.println("Our string is: " + schoolString); String subString01 = schoolString.substring(0,8); System.out.println("The 1st substring is: " + subString01); String subString02 = schoolString.substring(9); System.out.println("The 2nd substring is: " + subString02);
The output should be:
run: Our string is: Learning Glue The 1st substring is: Learning The 2nd substring is: Glue BUILD SUCCESSFUL (total time: 0 seconds)
Let’s move onto the final substring: The Phrase earning G. This contains the characters within the string Learning Glue. Since we need both a beginIndex and an endIndex we will need to could the number of characters from the start of the string to the first character of the substring, and then count the number of characters required by the substring + 1.
- L=0
- e=1 *beginIndex
- a=2
- r=3
- n=4
- i=5
- n=6
- g=7
- =8 *Remember spaces count too
- G=9 *endIndex
- l=10
- u=11
- e=12
So our beginIndex is 1 and endIndex is 9. You may recall that the beginIndex is inclusive and the endindex is exclusive so we need to add 1 to the endIndex giving us: substring(1,10). Let’s add this to our program:
String schoolString = "Learning Glue"; System.out.println("Our string is: " + schoolString); String subString01 = schoolString.substring(0,8); System.out.println("The 1st substring is: " + subString01); String subString02 = schoolString.substring(9); System.out.println("The 2nd substring is: " + subString02); String subString03 = schoolString.substring(1,10); System.out.println("The 3rd substring is: " + subString03);
The output should be:
run: Our string is: Learning Glue The 1st substring is: Learning The 2nd substring is: Glue The 3rd substring is: earning G BUILD SUCCESSFUL (total time: 0 seconds)