Home > Java > Java Basics > body text

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

Release: 2019-12-16 14:10:06
Original
14083 people have browsed it

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
Copy after login

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
Copy after login

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!

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 [email protected]
Popular Tutorials
More>
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!