The String class and its methods

We have the a variable, and this variable also acts an object. When we type a. in the editor, it'll show all the methods that are present in that String class, as shown in the following screenshot:

It reads the first character in the strings as index zero, the second character as index one, and so on. When working on a program, if you want the character present on index two, you can get it simply by using the following statement:

        Systme.out.println(a.charAt(2));

You print it in the output so that you will see that character. You might be wondering why would we need a single character from a string, but the charAt method is often used. In the next section, we will look at a program that can completely reverse the string.

For now, we will just go through an overview of the methods. We saw how to get a character that is present at a particular index position. Now let's try reversing this. Say that we have the character present and we need to find the index value at which the character is present in the string. We do this by using the indexOf method, shown as follows:

        Systme.out.println(a.indexOf"e"));

Run this program. You can see that the character l is at 2, H is at 0, e is at index 1, and l is at index 2. This is how you can extract characters and indexes with the help of the String methods.

But what if I want to pull the string only from the first character to the third character? Let's take a look at the following example:

        String a= "javatraining";
a.substring(3, 6);

We type a., and you can see that there is a substring. If you want to pull a string that starts at index 3 and ends at index 6, this means that j will be at 0, a will be at 1, and so on. It starts from 2, and moves on to 3, 4, and 5, and it will print something like vatra.

If you want to pull out substring from the entire string, then give the first letter index and the last letter index so that our entire string will be printed between that first and last letter. Bear in mind that there is another substring method, and with this method, if you don't pass the last index, passing only the first index, then it prints from index 5 to the last index, as follows:

        a.substring(5);

Let's print the output and see how the substring is extracted. The results of this are shown in the following screenshot:

Here, the index of e is -1, because there is no alphabetical character called e in this string. Whenever there is nothing, then it prints a -1.

That sums up substring. If I want to concat this string with one more string called rahul teaches, then I do the following:

        String a= "javatraining";
System.out.priontln(a.concat("rahul teaches"));

The javatraining string that is present in the a variable will be concatenated with rahul teaches, and it prints the output as javatrainingrahul teaches. We can also use a.length(), which will give the maximum length of this string starting from zero. There is one more type called trim. Let's say that there are some white spaces in your string, as follows:

        String a= " javatraining";
System.out.println(a.trim());

Here, the first character of the string is a blank space, and is then followed by the rest of the characters. If you want to remove that blank space, you can do so by simply using a.trim. This blank space is removed when you print the output.

If you want to print all the letters in uppercase, we can use a.toUpperCase. We can do the same for lowercase by using a.toLowerCase.

There is one more interesting method to look at, which is split. Basically, we can split the entire string based on our delimiter. For this, we use a.split(). In this case, we want to split it based on a slash in the code, as follows:

        String a= "java/training";
System.out.println(a.split(/));

This means that the whole string before the / character should be separated as one string and the remaining part should be separated as another string. This method can not only be used to split across a slash, but can also split across whatever we want it to, as shown in the following code:

        String a= "javatraining";
System.out.println(a.split(t));

If we want to split our string from t, then that means that java will be one string and raining will be another string. As we will have two strings, our output will store these two strings in an array, and this array return type will be, of course, a String, because it's written in a String, as shown in the following code:

        String arr[]=a.split("t");
System.out.println(arr[0]);
System.out.println(arr[1]);

If you want to print the first part of the string, then this will be stored in the 0 index of the array system, and if you want to print the second part of the string, then it will present it in the 1 index of the array.

One final method that we will discuss here is the replace method, shown in the following code:

        String a= "javatraining";
System.out.println(a.replace("t", "s"));

Here, we want to replace the t from the string with a random s. For this, we use a.replace("t", "s"), and that's it. On printing this, wherever a t is present in the string, it will be changed to an s, and it will be printed in your output.

That's pretty much it for String methods. You could still play around with them by using a. and go through different methods step by step, but these are the core methods that we use in our Java programming.

Let's try to tackle one example based on the methods that we have learned in this section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.169.50