Java intercepts the last few characters of a string
The String class in Java provides a substring(int from, int to)
method for intercepting the position from from to to-1 in the string character.
Because the character position of the string starts from 0, and the substring(int from, int to) method is closed in the front and open in the back, that is, [from, to), which can be understood as [from, to- 1]. At the same time, to can be omitted (polymorphism). By default, to intercepts the last digit of the string.
To intercept the last few characters in the string, you can first obtain the length of the string, and use the length-n of the string as the first parameter of the substring() method to intercept the string. The effect of the last n characters.
Example:
String str = "i like yanggb"; System.out.println(str.substring(str.length() - 6)); // yanggb
Also note that String in Java is an immutable type, so the substring() method does not change the original string, but returns a new one. String.
System.out.println(str); // i like yanggb
Recommended tutorial: Java tutorial
The above is the detailed content of Java intercepts the last few characters of a string. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1
Easy-to-use and free code editor