Manipulating Strings

Businessman using laptop in office
JamieB/RooM/Getty Images

The String class has several methods for manipulating the contents of a String. There can be a lot of times when this kind of String processing is useful. For example,  you might want to split a String containing a full name into a first name and second name or you might want to truncate a filename so that it doesn't have the filetype on the end.

Finding the Length of a String

A few of the String methods for String manipulation are based on the character index of a String.

The index is basically the position of each character within the String and it starts at zero. For example, the String "The Who" would have an index of T = 0, h = 1, e = 2, <space> = 3, W = 4, h =5, 0 = 6. As this character index is used so much, one of the most useful things to know about a String is its length. The String method length returns the number of characters in a string and is helpful in determining where the highest number the index goes to:

String bandName = "The Who";
System.out.println(("The Who".length())); 

which would display a result of 7 as there are seven characters in the String. This means the character index will go up to a value of 6 (don't forget it starts counting from 0).

Finding a Substring

It can be useful to find if a String contains a sequence of characters. For example, we could search the bandname variable for the String "Who". To look for the substring "Who" we can used the indexof method:

int index = bandName.indexOf("Who");

the result is an int specifying the index number - in this case it will be 4 as that is the position of the W character.

Now that we know the index we could truncate the bandName variable to remove the substring "Who". To do this we used the substring method. If we provide it with the starting index (in this case 0 as we want to start at the beginning of the String) and the ending index which is the position we just found:

String newBandName = bandName.substring(0,index);

this results in newBandName containing the string "The ".

Concatenating Strings

Two Strings can be added together to make a bigger String. There are a couple of ways to do this. The + operator is the easiest way:

newBandName = newBandName + "Clash";

resulting in newBandName containing the string "The Clash". The same result can be achieved by using the concat method:

newBandName = newBandName.concat("Clash");

The advantage of the + operator is you can add a few Strings together in one go:

String dog = "A" + "Great" + "Dane";

Trimming Strings

When working with Strings it can be quite common to come across leading and trailing spaces. A user might inadvertently enter in an extra space at the start or end of a text field or a program might read in some Strings that inadvertently have extra spaces attached. These spaces tend to get in the way of processing Strings so it can be a good idea to remove them. The String class provides a method called trim that does just that:

String tooManySpaces = "     Neil Armstrong..   ";
tooManySpaces = tooManySpaces.trim();

Now the tooManySpaces String contains "Neil Armstrong.." without being surrounded by spaces.

Example Java code can be found in the Fun With Strings Example Code.