如何使用 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