Home > Java > javaTutorial > How to use the indexOf() function of the String class in Java to find a specified character or substring in a string

How to use the indexOf() function of the String class in Java to find a specified character or substring in a string

PHPz
Release: 2023-07-24 18:13:07
Original
1114 people have browsed it

How does Java use the indexOf() function of the String class to find a specified character or substring in a string

Introduction:
In Java, the String class is one of the most commonly used classes. It provides There are many ways to manipulate strings. The indexOf() function is one of the methods used to find specified characters or substrings in a string. This article will introduce in detail how to use the indexOf() function of the String class in Java to implement string search operations, and provide some sample code to help readers better understand the usage of this method.

1. Introduction to the indexOf() function of the String class
The indexOf() function is one of the methods in the String class used to find specified characters or substrings in a string. Its signature is as follows:
public int indexOf(int ch)
public int indexOf(int ch, int fromIndex)
public int indexOf(String str)
public int indexOf(String str, int fromIndex)
Among them, ch represents the Unicode value of the character to be searched, str represents the substring to be searched, and fromIndex represents the starting position of the search.

The return value of the function is the index position of the found character or substring in the original string. If it is not found, -1 is returned.

2. Use the indexOf() function to find the specified character
The following is a sample code that demonstrates how to use the indexOf() function to find the position of the specified character in a string:

public class StringIndexOfExample {

    public static void main(String[] args) {
        String str = "Hello World!";
        char ch = 'o';
        int index = str.indexOf(ch);

        if (index != -1) {
            System.out.println("字符 " + ch + " 在字符串中的位置为:" + index);
        } else {
            System.out.println("未找到字符 " + ch);
        }
    }
}
Copy after login

Operation result:
The position of character o in the string is: 4

3. Use the indexOf() function to find the specified substring
The following is a sample code that demonstrates how to use indexOf() The function finds the position of the specified substring in the string:

public class StringIndexOfExample {

    public static void main(String[] args) {
        String str = "Hello World!";
        String subStr = "World";
        int index = str.indexOf(subStr);

        if (index != -1) {
            System.out.println("子串 " + subStr + " 在字符串中的位置为:" + index);
        } else {
            System.out.println("未找到子串 " + subStr);
        }
    }
}
Copy after login

Running result:
The position of the substring World in the string is: 6

4. Use the indexOf() function Find multiple positions of the specified character or substring
The indexOf() function can only find the first matching position of the specified character or substring in the original string. If you want to find multiple positions of a specified character or substring, you can use a loop to iterate over the string and call the indexOf() function repeatedly.

The following is a sample code that demonstrates how to use the indexOf() function to find multiple positions of a specified character or substring in a string:

public class StringIndexOfExample {

    public static void main(String[] args) {
        String str = "Hello World!";
        char ch = 'o';
        int index = str.indexOf(ch);

        while (index != -1) {
            System.out.println("字符 " + ch + " 在字符串中的位置为:" + index);
            index = str.indexOf(ch, index + 1);
        }
    }
}
Copy after login

Running result:
Character o The position in the string is: 4
The position of character o in the string is: 7

Summary:
This article introduces how to use the indexOf() function of the String class to find in Java A method for specifying characters or substrings in a string. As needed, we can find the position of the specified character or the position of the specified substring, and use the return value to determine whether the target character or substring is found. Additionally, if you want to find multiple matching positions, you can use a loop to iterate through the string and call the indexOf() function repeatedly. I hope this article will help readers understand and use the indexOf() function of the String class.

The above is the detailed content of How to use the indexOf() function of the String class in Java to find a specified character or substring in a string. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template