Home  >  Article  >  Java  >  How to get the substring of a string using the substring() method of the String class

How to get the substring of a string using the substring() method of the String class

PHPz
PHPzOriginal
2023-07-25 17:02:391201browse

How to use the substring() method of the String class to obtain the substring of a string

In Java programming, the String class is one of the most commonly used classes. It provides many useful methods to manipulate strings, one of which is the substring() method. Using the substring() method, you can obtain a substring from a string.

The substring() method has two ways of use: one is to pass in only one parameter, indicating the substring starting from the specified index position to the end of the string; the other is to pass in two parameters, indicating Starting and ending index, the returned substring includes the character at the starting index position, but does not include the character at the ending index position.

The following is an example code for using the substring() method to obtain a string substring:

public class SubstringExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        
        // 使用substring()方法获取从索引位置5开始到字符串末尾的子串
        String sub1 = str.substring(5);
        System.out.println("sub1: " + sub1); // 输出: World!
        
        // 使用substring()方法获取从索引位置0开始到索引位置5之间的子串
        String sub2 = str.substring(0, 5);
        System.out.println("sub2: " + sub2); // 输出: Hello
    }
}

In the above code, first declare a string str and its content for "Hello, World!". Then use the substring() method to get two different substrings.

The first substring starts from index position 5, which is the 6th character in the string, and goes to the end of the string. By passing in the single argument 5, we get the substring "World!".

The second substring starts from index position 0, which is the first character in the string, to the characters between index position 5. By passing in the two parameters 0 and 5, we get the substring "Hello".

It should be noted that the substring() method returns a new string object, and the original string object has not been modified. This is because the String class is an immutable class. Once a string object is created, its content cannot be changed.

In addition, it should be noted that the index parameter passed into the substring() method starts counting from 0, that is, the index of the first character is 0, the index of the second character is 1, and so on.

By using the substring() method, we can easily obtain any substring of the original string. This is very useful for dealing with string-related problems, such as extracting domain names from URLs, intercepting area codes in mobile phone numbers, getting file extensions, etc.

To summarize, using the substring() method of the String class, we can easily obtain the substring of the string and obtain the characters between different index positions as needed.

The above is the detailed content of How to get the substring of a string using the substring() method of the String class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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