在 Java 中查詢字串中某個字元的最後一個出現位置
使用最後一次方法來查詢 Java 中某個字串中某個字元的最後一個出現位置。
比方說以下字串為我們字串。
String myStr = "Amit Diwan";
在以上字串中,我們將找到字元“i”的最後一個出現位置
myStr.lastIndexOf('i');
以下為完整示例。
示例
public class Demo { public static void main(String[] args) { String myStr = "Amit Diwan"; int strLastIndex = 0; System.out.println("String: "+myStr); strLastIndex = myStr.lastIndexOf('i'); System.out.println("The last index of character a in the string: "+strLastIndex); } }
輸出
String: Amit Diwan The last index of character a in the string: 6
廣告