Java substring

Need to get part of a string? That’s what the Java substring method is for.

Substring method

We’re going to start with the substring method since it’s part of the AP Java Subset.

The Java substring method allows you to take a piece of one string out of another and returns it as a string. Let’s look at the following code.

String greeting = "Hello Bob!";
String sub = greeting.substring( 1 );
System.out.println( sub );

This is the first version of the substring method that you need to know for the AP exam. It takes a single integer parameter and returns everything in the source string starting at the position specified. Remember that string indexes start at 0, so greeting.substring(1) will return ello Bob! which is then stored in the sub variable. And that’s what’s printed.

Let’s play a bit. In the editor below is a bit of started code. You can play around with it and see what changing the value in the substring call will do. You might also want to play around and change the string greeting.

One thing to try is to put a number larger than 10 in the substring call and see what happens.

Submit

String greeting = "Hello Bob!"; String sub = greeting.substring( 1 ); System.out.println( sub );
>

Did you try a big number? You should have gotten a StringIndexOutOfBounds Exception. In Java you can’t ask substring for characters that are not in the string or you will get that exception.

Overloading

There is also an overloaded version of substring that takes 2 integer parameters. In this version substring still starts at the first parameter, but this time it doesn’t go to the end of the string. It goes up to, but doesn’t include the second parameter.

Time for a bit of code…

String greeting = "Hello Bob!";
String sub = greeting.substring(1, 8);
System.out.println( sub );

Can you guess what this will print? Why not try it?

Submit

String greeting = "Hello Bob!"; String sub = greeting.substring(1, 8); System.out.println( sub );
>

In this case it’s going to return ello Bo which is the characters starting at position 1 and going through position 7. It’s a very common confusion point that it does not include the last position.

One hint that a former student figured out is that the length of the string returned is the first parameter subtracted from the first. In this example 8 – 1 is 7, so the returned string would be 7 characters long. And sure enough, ello Bo is 7 characters. (The space counts).

And one quick note. When you’re using the 2 parameter version the second value has to be greater than or equal to the first. You can’t flip them.

charAt

Java strings also have a method named charAt that returns a single character from a string. But, unlike the substring method which returns a string, charAt returns a char.

And while charAt is a handy method to know, it’s not part of the AP Java Subset and isn’t directly tested on the AP exam. You’re free to use it on the free response questions, but you do need to be a bit careful as we’ll see in a bit.

String greeting = "Hello Bob!";
char ch = greeting.charAt( 3 );
System.out.println(ch);

This little snippet is going to print out the character in position 3, which is l.

Try playing a bit. While you’re playing, try to put a big number in. You should fin that it’ll do the same thing as using an out of bounds number in substring.

Submit

String greeting = "Hello Bob!"; char ch = greeting.charAt( 3 ); System.out.println( ch );
>

Careful though

We do need to be careful though with chars since internally, Java treats them like numbers.

Remember back when we learned about concatenating strings that we could put multiple strings together using the + operator. Confusingly that’s the same operator as we’d use for addition. And that’s where we have to be careful.

If we use the plus operator between 2 characters, instead of concatenating Java will add the ASCII values of the two characters. Let’s look at the following code.

String greeting = "Hello Bob!";
char c = greeting.charAt(1);
char d = greeting.charAt(4);
System.out.println( c + d );

It seems like this should print out eo. Try it. Does it?

Submit

String greeting = "Hello Bob!"; char c = greeting.charAt(1); char d = greeting.charAt(4); System.out.println(c + d);
>

212? Where did that come from?

If you look at an ASCII table you’ll see that a lower cased e has an ASCII value of 101 and l has a value of 111. Add those two together and you’ll get 212. Java is adding the ASCII values instead of concatenating.

There is a way to force it though, and that’s to tell Java to you want to concatenate strings by starting with a string.

String greeting = "Hello Bob!";
char c = greeting.charAt(1);
char d = greeting.charAt(4);
System.out.println( "" + c + d );

Adding an empty string before c + d tricks Java into treating into it all as a string and it concatenates like we want.

Java substring & the AP Exam

If you’re taking the AP exam this May you’re probably better off just sticking with the Java substring method and not using charAt at all. substring has the advantage that it returns a string and you won’t have to worry about Java adding up character values when you want it to concatenate strings.

If you only want a single character string from substring you just make sure that the second parameter is one larger than the first.

Submit

String greeting = "Hello Bob!"; String sub = greeting.substring(2, 3); System.out.println(sub);
>

This prints out the single character string l, but it’s a string and not a character.

Leave a Reply

Your email address will not be published. Required fields are marked *