Home > Java > Java Tutorial > body text

Use Java's String.lastIndexOf() function to find the last position of a character (string) in a string

WBOY
Release: 2023-07-25 21:57:09
Original
3259 people have browsed it

Use java's String.lastIndexOf() function to find the last position of a character (string) in a string

In java programming, string processing is a very common operation. Sometimes we need to find the last occurrence of a character or a string in another string. Java provides a lastIndexOf() function of the String class, which can easily implement this requirement. This article will introduce how to use the lastIndexOf() function and provide relevant code examples.

String.lastIndexOf() function is a method in the String class. Its function is to return the position of the last occurrence of the specified character (string) in the string. The function is declared as follows:

int lastIndexOf(int ch)
int lastIndexOf(String str)
Copy after login

The return value of the function is an integer, representing the index of the position. If the specified character (string) is not found, -1 is returned.

The following is a sample code that uses the lastIndexOf() function to find the last occurrence of a character in a string:

public class LastIndexOfExample {
    public static void main(String[] args) {
        String str = "Hello World!";
        char ch = 'o';

        int lastIndex = str.lastIndexOf(ch);

        if (lastIndex == -1) {
            System.out.println("字符 " + ch + " 没有在字符串中出现过。");
        } else {
            System.out.println("字符 " + ch + " 最后一次出现的位置是:" + lastIndex);
        }
    }
}
Copy after login

In the above sample code, we define a string str , and specifies the character ch to be found. Then use the lastIndexOf() function to find the position where the character ch last appeared in the string str, and save the result in the lastIndex variable.

Next, we use if-else statements to determine the results. If the value of lastIndex is -1, it means that the character ch has not appeared in the string str. Otherwise, the last occurrence of the characters ch will be printed.

To find the last occurrence of a string within another string, you can use a similar method. Just change the function parameter from character to string. The following is a sample code to find a substring:

public class LastIndexOfExample {
    public static void main(String[] args) {
        String str = "Hello World!";
        String subStr = "o";

        int lastIndex = str.lastIndexOf(subStr);

        if (lastIndex == -1) {
            System.out.println("子字符串 " + subStr + " 没有在字符串中出现过。");
        } else {
            System.out.println("子字符串 " + subStr + " 最后一次出现的位置是:" + lastIndex);
        }
    }
}
Copy after login

In the above sample code, we define a string str and specify the substring to find subStr. After that, we use the lastIndexOf() function to find the last occurrence of substring subStr in the string str and save the result in the lastIndex variable .

Finally, we also use the if-else statement to determine the result and print out the position where the substring subStr last appeared.

The above is the relevant content and sample code for using java's String.lastIndexOf() function to find the last position of a character (string) in a string. By learning and mastering the usage of this function, we can better handle string-related operations and improve programming efficiency.

The above is the detailed content of Use Java's String.lastIndexOf() function to find the last position of a character (string) in a string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!