如何使用 Java 从一个字符串提取最后 n 个字符?
要提取最后 n 个字符,只需使用 charAt() 方法打印从第 (length-n) 个字符到第 n 个字符。
示例
public class ExtractingCharactersFromStrings {
public static void main(String args[]) {
String str = "Hi welcome to tutorialspoint";
int n = 5;
int initial = str.length()-5;
for(int i=initial; i<str.length(); i++) {
System.out.println(str.charAt(i));
}
}
}输出
p o i n t
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP