Hi I think now you all are familiar with data types and variables. Therefore, I thought of focusing on Strings in this article.
As I have mentioned in the previous article, String is a reference data type. However, it is predefined by java. Therefore unlike other reference data types, we do not need to use new operator to allocate memory for String variables.
String roleName = "Edward Cullen";
You can simple store a value to a String variable as shown above. Nothing is difficult. But there are more interesting things about Strings. Since it is a reference type, strings have their own predefined methods that they can access by using the dot operator. Let's go through some of them.
You may want to know whether a particular string starts with a particular letter. For an example assume you are keeping details of a set of employees and you want to fetch the details of the employees having names starting with 'E'. So how can you know that?
String roleName = "Edward Cullen"; System.out.println(roleName.startsWith("E")); System.out.println(roleName.startsWith("e"));
As you can see they do not give same output. That is because the case sensitivity is under consideration here.
Just like that you also can check the ending letter as well.
String roleName = "Edward Cullen"; System.out.println(roleName.endsWith("n"));
You also can get the length of a string. Here length means 'number of characters' that a particular string has.
String roleName = "Edward Cullen"; System.out.println(roleName.length());
This will give the output as '13'. Notice that the whitespace in between 'Edward' and 'Cullen' has also been considered as a character.
Have you heard of the word 'index'? In strings the position of a particular character is said to be the index of that character in the given string. Though we start counting from '1' indexes in java starts with '0'.
So to find the index of a particular character in a given string, you can use;
String roleName = "Edward Cullen"; System.out.println(roleName.indexOf('l')); System.out.println(roleName.indexOf('j'));
This will give the out put '9' and '-1'. Let's discuss why. As you can see there are two 'l's in the string
roleName. However the method
indexOf only gives us the index of the first occurence of the given character. That is why it gives '9' as the first output. The next letter I have given is 'j'. But you can clearly see that letter 'j' is not present anywhere in the
roleName. Therefore the method returns '-1'. I think now you are clear with that. So let's proceed.
Can you guess the output of the following code lines without running them? Give it a try and see.
String roleName = "Edward Cullen"; System.out.println(roleName.replace("Edward", "Alice")); System.out.println(roleName.replace("l", "d")); System.out.println(roleName);
The first line will print "Alice Cullen". That is because the method
replace() has replaced the word "Edward" with "Alice". So when you are using the method
replace(), inside the parenthesis first you need to write the target word or letter that you want to replace and then write the word or letter of replacement. If I give it as a syntax;
The second line will print "Edward Cudden". That is because the string
roleName has two 'l's. So both of them gets replaced by a 'd'.
However has this
replace() method actually replaced the original string
roleName? No. By running the third line you will see the output as "Edward Cullen", which is the original string. So what happen here is that the method
replace() creates new objects whenever it is called. It does not change the original string. The main reason is that strings in java are immutable (cannot change).
Let's see few more string methods. You can convert a particular string into uppercase (capitalizing) and lowercase as well. Simple use the bellow methods;
String roleName = "Edward Cullen"; System.out.println(roleName.toUpperCase()); System.out.println(roleName.toLowerCase());
The outputs are "EDWARD CULLEN" and "edward cullen". Here also note that the original string will not get changed by any chance.
There's another very useful method called
trim(). This is used to remove the unnecessary white spaces that might be at the beginning or the end of a given string. Look at the given example.
You can clearly see that the extra white spaces in the original string has been removed by the
I think this is enough for this article. However, the methods I have mentioned above are NOT the only methods that are available for Strings. Use dot operator and explore more! You will find it interesting :)
Explore string methods and try to get the followings as outputs for the string that we used in above examples (roleName = "Edward Cullen").
- ard Cullen
- ard Cul
- Edward CuLlen (Notice here the third output only the first 'l' is 'L')
Check whether the substring "war" is present in the string "Edward Cullen".
Get the index of the substring "Cullen" in the string "Edward Cullen".
Given a string userName = "", check whether it is empty or not.