Home  >  Article  >  Java  >  How to use the substring() function of the String class in Java to implement string interception

How to use the substring() function of the String class in Java to implement string interception

WBOY
WBOYOriginal
2023-07-25 08:19:451622browse

How does Java use the substring() function of the String class to implement string interception

In Java programming, the String class provides many methods for processing strings. Among them, the substring() function is a very commonly used function, which can implement the interception operation of strings. In this article, I will introduce how to use the substring() function to implement string interception and provide some code examples.

First, let us understand the basic usage of the substring() function. The substring() function accepts one or two parameters, which are used to specify the starting position and ending position of the string to be intercepted. Its method signature is as follows:

public String substring(int beginIndex)

public String substring(int beginIndex, int endIndex)

Among them, beginIndex represents the starting position and endIndex Indicates the end position (not included in the intercepted string). If only one parameter, beginIndex, is passed, the substring() function will return the substring starting from beginIndex to the end of the string.

Now, let’s look at some practical examples to illustrate the usage of the substring() function.

Example 1:

String str = "Hello, world!";
String subStr = str.substring(7);
System.out.println(subStr); // 输出结果:world!

In the above example, we intercept the substring of the string "Hello, world!" starting from index 7, that is, from the 7th position of the string Starts with character "w". The intercepted result is "world!".

Example 2:

String str = "Java Programming";
String subStr = str.substring(5, 14);
System.out.println(subStr); // 输出结果:Programming

In Example 2, we intercept the substring of the string "Java Programming" starting from index 5 and ending at index 14 (exclusive). Therefore, the intercepted result is "Programming".

In addition to basic usage, we can also use the substring() function to implement some complex string interception operations.

Example 3: Intercepting the file name

String filePath = "/user/document/report.docx";
int lastIndex = filePath.lastIndexOf("/");
String fileName = filePath.substring(lastIndex + 1);
System.out.println(fileName); // 输出结果:report.docx

In Example 3, we intercept the file name from the file path. First, we use the lastIndexOf() function to get the position of the last slash ("/"). Then, we use the substring() function to intercept from position 1 and obtain the file name "report.docx".

Summary:

Through the above examples, we can see that the substring() function is a very common method for intercepting strings in Java. It can intercept strings by specifying the starting position and ending position, and can also implement some complex interception operations.

It should be noted that the substring() function returns a new string object rather than modifying the original string. Therefore, when using the substring() function to intercept a string, be sure to save the interception result in a new variable.

I hope this article will help you understand how to use the substring() function to implement string interception. If you have similar needs in actual development, you can operate according to the sample code provided in this article.

The above is the detailed content of How to use the substring() function of the String class in Java to implement string interception. 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