JavaRush /Java Blog /Random EN /The IndexOf method of the String class: why is it needed ...

The IndexOf method of the String class: why is it needed and how does it work

Published in the Random EN group
Hello! Programmers often use methods automatically without thinking about how they work. These are the basic methods that you must know.

What is method overloading?

Before we look at the method mentioned in the title, let's remember (and some will know) what method overloading is. Java method overloading allows two or more methods with the same name to be defined within the same class. This is only possible if they have different input parameters. In this case, the methods are called overloaded, and the process itself is called method overloading. The IndexOf method of the String class: why is it needed and how does it work - 1Method overloading is one of the fundamental principles of polymorphism in Java.

What is a indexOfclass method for?String

Imagine you have a long string. For example, a letter, and you need to find the place where the addressee is first addressed by a name that you already know. How will you do it? Use the indexOf javaclass method String. This method is a typical example of the method overloading we talked about above.

Method optionsindexOf

The method java string indexOfhas four different variations:

No. 1.indexOf(int ch)

The method int indexOf(int ch)returns the index in the given string of the first occurrence of the specified character. In other words, we will get the number of the first occurrence of the given character, counting from left to right. Example:
public static void main(String[] args) {
   String str = "Diego, where is my money?";
   int value = str.indexOf('e');
   System.out.println(value);
}
Console output:

2
If the character we are looking for is not in the given string, we will get -1.
public static void main(String[] args) {
   String str = "Diego, where is my money?";
   int value = str.indexOf('j');
   System.out.println(value);
}
Console output:

-1
PS In order not to confuse anything, you need to remember that the characters in the line are counted not from “‎1,2,3…”, but from “‎0,1,2...”‎

No. 2.indexOf(int ch, int fromIndex)

The method int indexOf(int ch, int fromIndex)returns the index in this string of the first occurrence of the specified character, starting the search at the specified index. This method is a more modernized version of the previous one. The difference is that we indicate the number of the element from which the search will actually begin. Example:
public static void main(String[] args) {
   String str = "Diego, where is my money?";
   int value = str.indexOf('e',5);
   System.out.println(value);
}
Console output:

9
If the index from which to search is greater than the length of the string, then the answer is -1. The IndexOf method of the String class: why is it needed and how does it work - 2As you may have noticed, in the signature of these two methods (what the methods take as arguments), the type for the character being passed is specified as int, not char. However, we have passed on char. This is because the strings are stored as an array of bytes, where the cells correspond to a certain character of type char. The correspondence between bytes and charis carried out according to the ASCII table. ASCII (American Standard Code for Information Interchange) is a table that maps common printable and non-printable characters to numeric codes. So when a character is passedchar, it is automatically converted to the number that the given character represents in the ASCII table. Based on this, we can directly pass a number ( int) to the method that corresponds to the character we need. For example, the character 'e' in the ASCII table corresponds to the number 101, so we can repeat the previous example, but without the char:
public static void main(String[] args) {
   String str = "Diego, where is my money?";
   int value = str.indexOf(101,5);
   System.out.println(value);
}
Our console output has not changed:

9
PS In some cases, these methods are interchangeable, as for example, str.indexOf('e');it will be similar - str.indexOf('e', 0);.

No. 3.indexOf(String str)

The method int indexOf(String str)returns the index in the given string of the first occurrence of the specified substring. It is fundamentally different from the first option in that this method is already looking for a whole substring ( String). Thanks to this, we can already look for something more specific. Example:
public static void main(String[] args) {
   String str = "Diego, where is my money? Diego,you made me very upset";
   int value = str.indexOf("Diego");
   System.out.println(value);
}
Console output:

0
PS All variations indexOfare case sensitive: upper case characters (large letters) and lower case characters (small letters) are treated as different. Be careful.

No. 4.indexOf(String str, int fromIndex)

The method indexOf(String str, int fromIndex)returns the index within this string of the first occurrence of the specified substring, starting at the specified index. This option is again a more modernized previous version, but with an indication of the place from which the search will be conducted. Example:
public static void main(String[] args) {
   String str = "Diego, where is my money? Diego,you made me very upset";
   int value = str.indexOf("Diego", 1);
   System.out.println(value);
}
Console output:

26
PS If you set a negative start index, the method indexOfwill take it as 0. In this case, the method will again become similar to the previous one (in which the start index is not specified). It's all for today. Now your stock of knowledge is bigger thanks to indexOfJava!The IndexOf method of the String class: why is it needed and how does it work - 3
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION