search
HomeJavajavaTutorialUse Java's String.substring() function to intercept the substring of a string

Use Java's String.substring() function to intercept the substring of a string

Jul 25, 2023 pm 09:06 PM
java programmingString interceptionsubstring function

Use java's String.substring() function to intercept a substring of a string

In the Java programming language, the String class provides a wealth of methods for operating strings. Among them, the String.substring() function is a commonly used method that can be used to intercept substrings of strings. This article will introduce how to use the String.substring() function to intercept strings, and provide some code examples for practical application scenarios.

  1. The syntax of the String.substring() function

The String.substring() function has two overloaded forms, which are used to intercept the substring starting from the specified position. and intercept the substring within the specified position range. The syntax is as follows:

1) Intercept the substring starting from the specified position:
public String substring(int beginIndex)

2) Intercept the substring within the specified position range:
public String substring(int beginIndex, int endIndex)

  1. Use the String.substring() function to intercept the substring of the string

You can use String.substring( ) function, intercepts substrings of strings according to requirements. The following are some common application scenarios and corresponding code examples:

Example 1: intercept the first few characters of the string

String str = "Hello world!";
String subStr = str.substring(0, 5); // 截取从索引为0到索引为4的子串
System.out.println("截取结果:" + subStr);

The output result is: Hello

Example 2 : Intercept the last few characters of the string

String str = "Hello world!";
int endIndex = str.length() - 1;
String subStr = str.substring(endIndex - 4, endIndex + 1); // 截取从倒数第5个字符到最后一个字符的子串
System.out.println("截取结果:" + subStr);

The output result is: world

Example 3: Intercept the substring in the middle of the string

String str = "Hello world!";
int beginIndex = str.indexOf("o"); // 定位'o'字符的索引
int endIndex = str.lastIndexOf("o"); // 定位最后一个'o'字符的索引
String subStr = str.substring(beginIndex, endIndex + 1); // 截取从第一个'o'字符到最后一个'o'字符的子串
System.out.println("截取结果:" + subStr);

The output result is: o worl

  1. Notes

When using the String.substring() function, you need to pay attention to the following points:

1) The parameters beginIndex and endIndex represent respectively Intercept the starting position and ending position of the substring. Among them, beginIndex represents the starting index of the substring (inclusive), and endIndex represents the ending index of the substring (exclusive).
2) Both beginIndex and endIndex must be within the valid index range of the string, otherwise a StringIndexOutOfBoundsException exception will be thrown.
3) When endIndex is not specified, the intercepted substring contains all characters after the beginIndex position.
4) When beginIndex and endIndex are equal, the intercepted substring is an empty string.

Summary

The String.substring() function is a common method for intercepting string substrings in Java. By specifying the starting position and ending position, you can flexibly intercept the required substring. In actual programming, you can use the String.substring() function to process strings according to different needs, such as interception, extraction, and judgment operations. The code examples provided above can help readers better understand and apply the String.substring() function.

The above is the detailed content of Use Java's String.substring() function to intercept the substring of a string. 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)