Home > Java > javaTutorial > body text

How does Java use the substring() function of the String class to obtain a substring of a string while specifying the starting position and ending position?

PHPz
Release: 2023-07-24 23:18:14
Original
1334 people have browsed it

How does Java use the substring() function of the String class to obtain a substring of a string and specify the starting position and end position at the same time

String is one of the data types often used in daily programming. In Java, the String class is a class used to represent strings, and it provides many methods for manipulating strings. One of the commonly used methods is the substring() function, which is used to obtain the substring of a string.

The definition of the substring() function is as follows:

public String substring(int startIndex, int endIndex)
Copy after login

This function accepts two parameters, namely the starting position startIndex and the ending position endIndex. It returns a new string that is a substring of the original string, ranging from startIndex to endIndex-1.

The following is a sample code that uses the substring() function to obtain a string substring:

public class SubstringExample {
    public static void main(String[] args) {
        String str = "Hello, World!";

        // 指定起始位置为6,结束位置为12
        String subStr = str.substring(6, 12);

        System.out.println(subStr); // 输出 "World"
    }
}
Copy after login

In the above example, we declared a string variable str whose value is "Hello , World!". Then, we use the substring() function to obtain the substring of str. The starting position is 6 (that is, the position of the character 'W') and the ending position is 12 (that is, the position of the character '!'). Finally, we print out the obtained substring.

It should be noted that both the starting position and the ending position start counting from 0. Therefore, the character 'W' is at position 6 instead of 7, and the character '!' is at position 12 instead of 13.

In addition to using specific starting and ending positions, we can also use the overloaded method of the substring() function to specify only the starting position but not the ending position. In this way, the function will intercept from the specified starting position to the end of the string. An example is as follows:

public class SubstringExample {
    public static void main(String[] args) {
        String str = "Hello, World!";

        // 指定起始位置为7
        String subStr = str.substring(7);

        System.out.println(subStr); // 输出 "World!"
    }
}
Copy after login

In the above example, we only specified the starting position as 7 and did not specify the ending position. Therefore, the substring() function will intercept from the starting position 7 to the end of the string. The last printed substring is "World!".

Using the substring() function can easily obtain the substring of a string, which is very practical for string operations and processing. When we need to process part of a string, we can use the substring() function to quickly obtain the substring. I hope the above code example can help you understand the use of substring() function.

The above is the detailed content of How does Java use the substring() function of the String class to obtain a substring of a string while specifying the starting position and ending position?. 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