Home> Java> javaTutorial> body text

How to use the substring() function of the String class in Java to intercept the substring of a string

王林
Release: 2023-07-25 16:34:56
Original
722 people have browsed it

How does Java use the substring() function of the String class to intercept the substring of a string

In Java, we often need to perform some processing on strings, one of which is to intercept the substring of a string. Java provides the substring() function of the String class to facilitate us to implement this operation. This article will introduce how to use the substring() function to intercept the substring of a string, and attach a code example.

The substring() function is a commonly used function in the String class. It has two different overloaded forms:

  1. substring(int beginIndex): Change the string from the specified index The position is cut from the beginning to the end.
  2. substring(int beginIndex, int endIndex): intercept the string from the specified start index position to the character before the specified end index position.

The following is a simple example that demonstrates how to use the substring() function to intercept a substring of a string:

public class SubstringExample { public static void main(String[] args) { String str = "Hello World"; // 使用substring(int beginIndex)截取字符串从指定索引位置开始到末尾的子串 String sub1 = str.substring(6); System.out.println("截取结果1:" + sub1); // 输出:World // 使用substring(int beginIndex, int endIndex)截取字符串从指定开始索引位置到指定结束索引位置的前一个字符的子串 String sub2 = str.substring(0, 5); System.out.println("截取结果2:" + sub2); // 输出:Hello } }
Copy after login

In the above example, we define a string str whose value is "Hello World". Then we used the substring() function to intercept two substrings.

First, we use the substring(int beginIndex) function and specify beginIndex as 6, which means that the string will be intercepted starting from index position 6. This means that it will start from the 7th character of the string and continue to the end. Finally, store the intercepted substring in the sub1 variable and output the result to the console.

Next, we use the substring(int beginIndex, int endIndex) function, specifying beginIndex as 0 and endIndex as 5, which means that the string will be intercepted from index position 0 to the previous character at index position 5. This means that the first 6 characters of the string will be intercepted. Finally, store the intercepted substring in the sub2 variable and output the result to the console.

By running the above sample code, we can get the following output:

截取结果1:World 截取结果2:Hello
Copy after login

As you can see, by using the substring() function of the String class, we can easily intercept the substring of the string. It should be noted that the substring() function returns a new string object, and the original string object has not changed.

To summarize, it is very simple to use the substring() function of the String class to intercept the substring of a string. We only need to specify the corresponding index position to implement the interception operation. I hope the content of this article will be helpful to everyone when using the substring() function.

The above is the detailed content of How to use the substring() function of the String class in Java to intercept the substring of 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 admin@php.cn
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!