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