Home  >  Article  >  Java  >  How to intercept the last few characters of a string in java

How to intercept the last few characters of a string in java

尚
Original
2019-12-16 14:10:0614304browse

How to intercept the last few characters of a string in java

How to intercept the last few characters of a string in java:

To intercept the last few characters of a string in java, you can use the substring method of the String class, please refer to the instructions below for specific usage:

1. substring(int beginIndex)

substring(int beginIndex)

Returns a new string that is a substring of this string. Intercept

Intercept all characters starting from string position beginIndex-1 and later. Note that the starting position of the characters starts from 0.

Example:

String name="weicc-20100107-00001";
System.out.println(name.substring(name.length()-5));//输出00001

2. substring(int beginIndex, int endIndex)

substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string.

Intercept all characters from the string position beginIndex to the position endIndex-1. This method is closed at the beginning and opened at the end, that is, including the characters at the beginIndex position. But the character at endIndex position is not included.

Example:

String name="weicc-20100107-00001";
System.out.println(name.substring(name.length()-5,name.length()));//输出00001

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of How to intercept the last few characters of a string in java. 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