Useful String Methods - Part 2

Start course
Overview
Difficulty
Beginner
Duration
1h 31m
Students
28
Ratings
5/5
starstarstarstarstar
Description

In this course, we'll learn Strings in Java. 

Learning Objectives

  • What is String?
  • Creating String Objectives
  • Useful Methods of String Class
  • Immutable String
  • StringBuffer Class
  • StringBuilder Class

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there, in our previous lesson, we talked about some methods used in the String class. In this lesson, we will practice using the methods in the String class. If you're ready, let's start. First, let's create a new class. I right click on the string examples package and select the new class options. The class name can be string method examples, and I will check the check box for the main method, and click the finish button. First, I will create three string objects. The name of the first string can be myString1, and its value will be Java programming. The name of the second string can be myString2, and its value will be Language. The name of the third string can be myString3, and its value will be language with lowercase letters. The first method we will examine will be the index of method. This method will take the char parameter and give us the index number of this character. So, let's see, System.out.println (myString1.indexOf(''v'')). 

So, the value of the myString object is Java Programming, in this string is 16 characters, and the index of the first character is zero, and the index of the last character is 15. So, the index number of the v will be two. Let's run and see. As you can see, the index of the v is two. Let's copy this line and paste it once. Before continuing, lets write the phrase Java Programming here, and write the index number of each letter below. Thus we can see which letter has which index number more easily, and we can better understand the string methods we'll use. Yes , the first index will be zero. Space is also considered a character. We should write the index number on it, and the final index number is 15. Yes, we can continue now. I will change the v Letter to a, and this time, I'll give the second parameter to be indexOf method, I will write six. This means look at the character a from the 6th index, and give the index number of the first character a you come across. 

And this time, it will print the 10 because the index number of the a from the 6th index is 10. Let's run and see. As you can see, we printed the 10, I will copy and paste this print method once again. This time I write gram for the first parameter of the indexOf method, and I will delete the second parameter. This means it will give us the index number where the word gram starts. So, we will print the eight because the word gram starts with the eighth index. Let's run and see. As you can see, it printed the eight. And I'll copy and paste this print method one more time. And I will give a second parameter to the indexOf method. I write seven. This time it looks from the seventh index. So, it will print eight again. Let's run and see, as you can see, it printed eight again. What if the character or word you want to know

the index number of is not in the string? In this case, it returns -1. For example, let's change the second parameter in the last print method to nine. Because there is no gram expression after the 9th index, in this case the value -1 will be printed to the console. Let's run it and see, as you can see, -1 is printed to the console. Also, the index number we wrote in the 2nd Parameter maybe outside the index number of the index. For example, if we type -5 here, eight will be printed to the console again. Let's run it, As you can see, we got the results eight. Now,. let's change the second parameter to 20, and run it again. As you can see , this time the value -1 is printed because after the 20th index , it could not find such a word. Yes, the indexOf method is like this. Now, let's look at the second method charAt. This method will give us a character at the specific index. 

For example, let's find the character at the zeroth index of the myString number. System.out.println(myString1.charAt (0)) let's run and see. As you can see, it printed the J letter. We said the Java also accept spaces as characters. Now, let's write the index number of the space separating the words Java and Programming. Let's see what it will print to the console. I will copy and paste this code, notice that the index number of the space is four.  So I write (4) and let's run it. As you can see, it printed a space. Well, what happens if we write an index number other than the index numbers of the string? Let's see, I'll copy and paste this print method. For example, let's write 20 here, and run it again. As you can see this time we get an error. This error is string index out of bounds exception, so this index is out of range. The charAt method is like this. 

Now, let's look at the other method, the substring method. This method returns a string that is a substring of the string. The substring begins at the specified begin index and extends to the character at Index end Index -1. Thus, the length of the sub string is end index minus begin index. Let's make some examples with the substring method. System.out.println (myString1.substring(5)). This prints the elements from the fifth index. So, it will print the programming word because the letter in the fifth index is P. So, it will print the letter P, and the other letters following the P, I.e. Programming word. So, let's run and see. First, let's convert this line to the comment line. Now, we can run it . As you can see, it printed the Programming word. If we want, we can specify the last index of the substring. So, I'll copy and paste this print method. For the second parameter, I write 12. So, this time I'll print the program word on the console. So, the index of the P Is five, and the index of the M Is 11. 

The second parameter should be one more than the index number of the last letter we want to print. So, the last letter is M and its index number is 11, for this we must write 12. This is important because we can write 16 here. and we cannot make any errors. Normally , the index number of this string is 15, but you may think that you can get an error when 16 is written here. Sixteen is greater than the index number but you will not get an error, because if we write 16 as the second parameter, it will look at the fifteenth index number 16 - 1 = 15. So, this string is 15 characters, and we cannot make any errors. Now, I write 12 again, and let's run and see. As you can see, this time the program word is printed, the substring method is like this. Now, let's look at another method, the equalsIgnoreCase method. If you remember, we learned the equals method of the class, and we said that Java is case sensitive. So, if you match the Java with the lowercase letters, and the Java with the first letter uppercase using the equals method, the result will be false. 

If we use the equalsIgnoreCase method, it does an equalization regardless of whether the letters are uppercase or lower case. And in this case, when we compare the same words, we get the result true. Now, let's take an example. I will compare the myString2 and myString3 objects. The first letter of the myString2 is uppercase, and the first letter of the myString3 is lower case. So, let's compare them. System.out.println (MyString2.equalsIgnoreCase(myString3)), the result must be true. Let's run and see. As you can see, it's printed "true" on the console. Okay. Now let's look at another method, the endsWith method. This method checks if the string ends with the character or phrase you provided. It returns "true" if it is ending and "false" if not. Let's just do an example. System.out.println(myString1.endsWith ("ing")). You can write a character or a word. If the myString is ending with the "ing" it will return "true", else return "false". So, now it will print "true" because the Java programming ends with "ing". Let's run and see. As you can see, it returns "true". Yes. 

Now let's look at another method, the startsWith method. This method checks if the string starts with the character or phrase you provided. It returns "true" if it is starting and "false" if it is not. Let's just do an example. System.out.println(myString2.startsWith ("l".toUpperCase)). So, first it will convert it to uppercase and then checks the start letter. You can write a character or a word. If the myString2 is starting with the L, it will return "true", else return "false". So, now it will print "true" because the language starts with L. Let's run and see. As you can see, the result is "true". Now, let's look at another method, the contains. This method returns "true" if and only this string contains the specified sequence of char values. Let's just do an example. System.out.println(myString1.contains ("gr")). So, if the myString1 contains the "gr", it will return "true", else "false". Let's run and see. As you can see, it's returned "true" because the myString1 contains "gr". Let's do another example. 

I'll copy and paste this print method. And this will be ("J".toLowerCase). So, this time first it converts the to the lowercase and then looks in the myString1. Since the myString1 does not contain J with lowercase, it will return "false". Let's run and see. As you can see, it's returned "false". Okay. Let's look at another method, the replace method. This method replaces each sub-string of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end. For example, replacing 'aa' with 'b' in the string 'aaa' will result in 'ba' rather than 'ab'. Let's do an example. Let's replace space in Java programming with underscore. System.out.println(myString1.replace). The first parameter will be the target, so, the "space", and the second parameter will be the replacement, so, the "underscore". Let's run and see. As you can see, it's printed as "Java_Programming", and I'll copy this print method and paste it here. This time I'll change the "m" letter with the underscore. But first, let's add one more "m" here. 

And I will write "mm" for the target parameter of the replacement method. If you notice, there are three "m" letters in the myString object, and we want to change the first two "m" letters. Let's run and see. As you can see, the first two "m" letters are changed because the replacement proceeds from the beginning of the string to the end, and instead of the "mm" it wrote underscore. Yes. Let's look at another method, the trim method. This method may be used to trim space from the beginning and end of a string. Let's do an example. System.out.println(" java programming language "). And I will copy and paste this line, and I will add the trim method at the end of this string. Let's run and see. As you can see, the first string is printed with spaces, a space at the beginning and two spaces at the end, but the other string is printed without a space. So, it removed the spaces at the beginning and the end. Okay. The trim method is like this. 

Finally, I want to remind you of one more thing. If you notice, we have made changes to the myString1 object using many methods. Now, finally, let's print the myString1 object itself to the console. Let's see if we encounter the value we defined at the beginning. Let's run the code. As you can see, myString1 remains the same, unchanged. This is because strings are immutable. But if you transfer a change you made in any line to a new object, then the string will change because it is now a new object. After reminding you of this, we can end our lesson. See you in the next lesson.

 

About the Author
Students
458
Courses
47
Learning Paths
3

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics