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

IndexOf method of the String class: why it is needed and how it works

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

What is method overloading?

Before we look at the method mentioned in the title, let's remember (and someone 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 it is needed and how it works - 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 first address to the addressee takes place by a name that you already know. How will you do this? Use the indexOf javaclass method String. This method is a typical example of method overloading, which we discussed 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 a 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 this line, 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 must not forget that the characters in a 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 the search will be conducted exceeds the length of the string, then the answer will be -1. IndexOf method of the String class: why it is needed and how it works - 2As you may have noticed, in the signature of these two methods (what the methods take as arguments), the type for the symbol being passed is specified as int, not char. However, we transmitted char. This is because strings are stored as a byte array, where cells correspond to a specific character of type char. The correspondence between the bytes 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. Therefore, when a certain character is passed char, it is automatically converted into a number that represents that character in the ASCII table. Based on this, we can directly pass the number ( int) to the method, which 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 row of the first occurrence of the specified substring. It is fundamentally different from the first option in that this method already searches for an entire substring ( String). Thanks to this, we can 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: uppercase characters (capital letters) and lowercase characters (small letters) are considered different. Be careful.

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

The method indexOf(String str, int fromIndex)returns the index in this row of the first occurrence of the specified substring, starting at the specified index. This option is again a more modernized previous option, 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 specify a negative start index, the method indexOfwill perceive it as 0. In this case, the method will again be similar to the previous one (in which the start index is not specified). It's all for today. Now you have more knowledge thanks to indexOfJava!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION