Skip to main content

JAVA Strings Methods

JAVA Strings Methods


  Ø  Java and String
  Ø  Upper and LowerCase
  Ø  The compare Method
  Ø  The indexOf Method
  Ø  The substring Method
  Ø  The equals Method
  Ø  The charAt Method
  Ø  The replace Method

Java and String
There's more to strings than meets the eye. Unlike int variables, or double variables, strings are objects. What this means in practice is that you can do things with strings of text that you can't do with int or double variables. (The same applies to the primitive data types boolean, byte, single, char, float, long and short: they are not objects like strings are.)
Before we get to manipulating strings of text, here's some basic information on what strings actually are.

How Java Stores Strings

A string is a series of Unicode characters held under a variable name. Take the following string:


String someText = "Bill";

This tells Java to set up a string object with the four characters "B", "i", "l" and another "l". In the Unicode character set, these values are: \u0042, \u0069, \u006c, \u006c. Unicode values are stored as hexadecimals numbers. Capital letters (A to Z) are stored using the values \u0041 to \u005a, while lowercase letters (a to z) are stored using the hexadecimals values \u0061 to \u007a.
In the previous section, we had an array which held strings of text. We then sorted the array:

When the programme is run, the output is this:

We noted that the word "This" comes first. If the array is supposed to be sorted alphabetically, however, you would expect the word "a" to come first. The reason it doesn't is because lowercase "a" has a hexadecimal value of u\0061, which is the decimal number 97. But uppercase "T" has a hexadecimal value of u\0054, which is the decimal number 84. 84 is lower than 97, so the "T" comes first.


Upper and Lower Case

Start a new project for this, and add the following code:

The first two lines of code just set up a String variable to hold the text "text to change", and then we print it out. The third line sets of a second String variable called result. The fourth line is where we do the converting:

result = changeCase.toUpperCase( );

To use a string method you first type the string you want to work on. For us, this was the string in the variable called changeCase. Type a dot after the variable name and you'll see a list of available methods that you can use on your string. Select toUpperCase. (The method needs the empty round brackets after it.)
After Java has changed the word to uppercase letters, we're storing the new string into our result variable.
When the programme is run, the Output window displays the following:

But you don't have to store the converted word in a new variable. This would work just as well:
System.out.println( changeCase.toUpperCase( ) );
Here, Java will just get on with converting the string, without needing to assign the result to a new variable.
If you want to convert to lowercase, just use the toLowerCase method instead. It is used in exactly the same way as toUpperCase.


The compare Method
You can compare one string to another. (When comparing, Java will use the hexadecimal values rather than the letters themselves.) For example, if you wanted to compare the word "Ape" with the word "App" to see which should come first, you can use an inbuilt string method called compareTo. Let's see how it works.

We've set up two string variables to contain the words "Ape" and "App". The compareTo method is then this line in the code above:


result = Word1.compareTo( Word2 );

The compareTo method returns a value. The value that is returned will be greater than 0, less than 0, or have a value of zero. If Word1 comes before Word2, then the value that is returned will be less than 0. If Word1 comes after Word2 then the value returned will be greater than 0. If the two words are identical then a value of 0 will be returned.
So you need to assign the value that compareTo returns to a variable. We're placing the value in an integer variable called result. The IF Statements in the code simply tests to see what is in the result variable
However, when you compare one string of text with another, Java compares the underlying hexadecimals values, rather than the actual letters. Because uppercase letters have a lower hexadecimal value than lowercase ones, an uppercase letter "A" in "App" will come before a lowercase letter "a" in "ape". Try it for yourself. Change "Ape" to "ape" in your code. The Output will read "Word1 is more than Word2", meaning that Java will place the word "ape" after the word "app" alphabetically.
To solve the problem, there's a related method called compareToIgnoreCase. As its name suggest, lowercase and uppercase letter are ignored. Use this and "ape will come before "App" alphabetically.


The indexOf Method
The indexOf method is used to locate a character or string within another string. For example, you can use it to see if there is a @ character in an email address. Let's use that example in some code.


We want to check if the @ sign is in the email address, so we first set up a char variable and assign it a value of '@'. (Note the single quotes for the char variable). After setting up an email address, we have a result variable. This is an int variable. The reason that result is an integer is because the indexOf method will return a value. It will return the position number of the ampersand character in the string email_address. Here's the relevant line:

result = email_address.indexOf( ampersand );

The string you're trying to search comes first. After a dot, type indexOf. In between the round brackets of indexOf, you have several options. One of the options is to type a single character (or the name of char variable). We've placed our ampersand variable between the round brackets of indexOf. Java will then tell us the position of the @ character in the email address. It will store the value in the result variable.
When you run the code, the output will be 4. You might think that the @ sign is the fifth character in the email address. But indexOf starts counting at 0.
However, if the character is not in the word that you're searching, indexOf will return a value of -1. To test this out, delete the @ sign from your email_address string. Then run your code again. You'll see -1 as the output.
You can use the return value of -1 to your advantage. Here's the code again, only with an IF statement that examines the value of the result variable:

So if the result of indexOf is -1 then we can do one thing, else allow the user to continue.
You can also use indexOf to test for more than one character. The code below checks the email address to see if it ends with ".com":

The code is almost identical, except we're now using a String variable to hold the text we want to check for (.com), and not a char variable.
Again, a result of -1 will be returned if the text to search for is not found in the String that comes before the dot of indexOf. Otherwise, indexOf will return the position of the first of the matching character. In the code above, the dot is the seventh character of the email address, when you start counting from 0.
You can also specify a starting position for your searches. In our email address example, we can start searching for the ".com" after the @ symbol. Here's some code that first locates the position of the @ symbol, and then uses that as the start position to search for ".com".

The new line of code is this one:

result = email_address.indexOf( dotCom, atPos );

The only thing different is the addition of an extra variable between the brackets of indexOf. We still have the string we want to search for (which is whatever text is in the dotcom variable), but we now have a starting position for the search. This is the value of the variable called atPos. We get the atPos value by using indexOf to locate the position of the @ symbol in the email address. Java will then start the search from this position, rather than starting at 0, which is the default.

Ends With … Starts With

For the programme above, you can also use the inbuilt method endsWith:

Boolean ending = email_address.endsWith( dotcom );

You need to set up a Boolean variable for endsWith, because the method returns an answer of true or false. The string you're trying to test goes between the round brackets of endsWith, and the text you're searching goes before it. If the text is in the search string then a value of true is returned, else it will be false. You can add an if … else statement to check the value:

if (ending == false ) {
System.out.println( "Invalid Email Address" );
}
else {
System.out.println( "Email Address OK " );
}

The method startsWith is used in a similar way:


Boolean startVal = email_address.startsWith( dotcom );
Again, the return value is a Boolean true or false.


Substring
One really useful method available to you is called substring. This method allows you to grab one chunk of text from another. In our email address programme above, for example, we could grab the last five characters from the address and see if it is co.uk.
To get some practice with substring, we'll write a small Name Swapper game. For this game, we want to change the first two letters of a family name and swap them with the first two letters of a personal name, and vice versa. So if we have this name:
"Bill Gates"
we would swap the "Ga" of "Gates" with the "Bi" of "Bill" to make "Bites". The "Bi" of "Bill" will then be swapped with the "Ga" of "Gates" to make "Gall". The new name printed out would be: "Gall Bites"
We'll use substring for most of this programme. Substring works like this:

String FullName = "Bill Gates";
String FirstNameChars = "";
FirstNameChars = FullName.substring( 0, 2 );

You set up a string to search, in this case the string "Bill Gates". The string you're trying to search goes after an equals sign. After a dot, type the name of the method, substring. There are two ways to use substring, and the difference is in the numbers between the round brackets. We have two numbers in the code above, 0 and 2. This means start grabbing characters at position 0 in the string, and stop grabbing when you have two of them. The two characters are then returned and placed in the variable FirstNameChars. If you always want to go right to the end of the string, you can just do this:

String test = FullName.substring( 2 );

This time, we only have 1 number between the round brackets of substring. Now, Java will start at character two in the string FirstName, and then grab the characters from position 2 right to the end of the string.
Start a new programme to test this out. Add a print line to the end and your code should be this:

When the programme runs, the Output window should look like this:

So the substring method has allowed us to grab the first two characters of the name "Bill".
To get the first characters, we had a 0 and a 2 between the round brackets of substring. You might think that to get the "Ga" of "Gates" that we could just do this:

= FullName.substring(5, 2);

We still want two characters, after all. Only this time, the 5 would tell Java to start from the "G" of "Gates". (The first position in a string is position 0 not position 1.) So, start at position 5 in the string and grab 2 characters.
However, running that code would get you an error. That's because the second number between the round brackets of substring doesn't mean how many characters you want to grab. It means the position in the string that you want to end at. By specifying 2 we're telling Java to end at the character in position 2 of the string. As you can't go from position 6 backwards to position 2 you get an error instead.
(NOTE: If you start the count at 0 in the string "Bill", you might think that position 2 is the letter "l". And you'd be right. But substring starts before the character at that position, not after it.)
To get the "Ga" of "Gates", therefore, you could do this:

FullName.substring( 5, FullName.length( ) - 3 );

The second number is now the length of the string minus 3 characters. The length of a string is how many characters it has. "Bill Gates" has 10 characters, including the space. Take away 3 and you have 7. So we're telling substring to start at character 5 and end at character 7.
And that would work perfectly well for people called "Bill Gates". But the programme wouldn't work if you name was, say, "Billy Gates". The code above would then grab the space character plus the letter "G", which is not what we want at all. We want the programme to work whichever two names are entered. So we have to get a bit clever.
One thing we can do is to note the position of the space in the two names. The 2 characters we want to grab from the second name always come right after the space character. We want some code that grabs those first two characters after the space.
We can use indexOf to note the position of the space:

int spacePos = FullName.indexOf(" ");

To specify a space character you can type a space between two double quotes (or single quotes). This then goes between the round brackets of indexOf. The value returned will be an integer, and it is the position of the first occurrence of the space character in the string FullName.
Test it out by adding the line above to your code: Add a print line to check the Output:

Run the programme to see the following Output:

So the space is at position 4 in the string. We can use this fact to grab the first two characters of "Gates", or indeed any second name. We tell Java to go from the first character after the space, and end at the next two characters:

FullName.substring( spacePos + 1, (spacePos + 1) + 2)

So the two numbers between the round brackets of substring are these:

spacePos + 1, (spacePos + 1) + 2

We want to start at the first character after the space (space + 1), and end two characters after this position, which is (spacePos + 1) + 2.
Add the following lines to your code (The ones highlighted. Our new substring method spills over on to two lines, but you can keep your on one, if you prefer):

When you run the programme, the Output window is this:

So we now have the "Bi" from Bill and the "Ga" from Gates. What we now need to do is get the rest of the characters from the two names, and then swap them around.


The equals Method in Java
You can check two strings to see if they are the same. For this, use the equals method in Java. Here's some code:

In this code, we want to check if one email address is the same as another. The first two lines set up two string variables, one for each email address. The third line sets up a Boolean variable. That's because the equals method returns a value of true or false. The fourth line is where we use the method:

isMatch = email_address1.equals( email_address2 );

In between the round brackets of the equals method, you place the string you're trying to check. The other string goes before the equals method. Java will then tell you (true or false) whether the two are the same. The IF statement checks which one it is.
The equals method only compares objects, however. It's OK for strings, because they are objects. But you can't use the equals method to compare int variables. For example, this code would get you an error:

int num1 = 12;
int num2 = 13
Boolean isMatch = false;
isMatch = num1.equals(num2);

The int variable is a primitive data type, and not an object. You can turn the primitive int data type into an object, though:

int num1 = 12;
Integer num_1 = new Integer(num1);
Here, the int variable called num1 is being turned into an Integer object. Note the use of the new keyword. In between the round brackets of Integer, you put the primitive int data type you want to convert to an object.


The charAt method in Java

You can check to see which single character is in a particular string. The charAt method is used for this in Java. Here's some code to try:

String email_address = "meme@me.com";
char aChar = email_address.charAt( 4 );
System.out.println( aChar );

This code checks which letter as at position 4 in the email address string. The return value is a variable of type char:

char aChar = email_address.charAt( 4 );

When the above code is run, the output is the character @. The number between the round brackets of charAt is the position in the string you're trying to check. Here, we want to get at the character in position 4 of the email_address string. Again, the count starts at 0, just like substring.
One good use for charAt is for taking a letter from a string variable that is typed by a user, and then converting it to a single char variable. For example, you could ask the user to type Y to continue or an N to exit. Have a look at this code:

We can't use the Scanner class directly to get a single letter to store in a char variable. So we use the next( ) method to get the next string that the user inputs. There's a next integer, next long, next double - even a next Boolean. But there's no next char. Even if the user inputs a single character it will still be a string and not a char. (Remember: a char variable stores a Unicode number as an integer.)
We can use charAt to get a character from any string that the user inputs, even if the user inputs a single letter:

char aChar = aString.charAt( 0 );

All we're saying is "Get the character at position 0 in the string called aString, then store it in the aChar variable".


The replace Method in Java

The replace method in Java is used to replace all occurrence of a character/s in a particular string. Take this sentence:
"Where are you books?"
We want to replace "you" with "your". Here's the code:


There are several ways to use the replace method, and they differ in what you put between the round brackets of the method. We're replacing one sequence of characters with another. Think of the comma separating the two as the word "with". You'd then have "Replace you with your".
You can also replace single character:


aString.replace( '£', '@' )

The above code reads "Replace £ with @".
(You can also use something called a regular expression in your replace methods, but that is outside the scope of this book.)

Trim

You can trim white space from your strings White space is things like space characters, tabs, and newline characters - the characters you can't see, in other words. The trim method is easy to use:
String amend = " white space ";
amend = amend.trim( );
So the trim method goes after the string you want to amend. The blank characters before the word "white" and after "space" in the code above will then be deleted.
If you're getting input from a user then it's always a good idea to use trim on the inputted strings.

Comments