1. 程式人生 > >確定字串中某字元出現多少次,所有索引

確定字串中某字元出現多少次,所有索引

1.出現多少次: 判斷某個字元有幾個 String a = “abbdfsads”; int n = a.length()-a.replaceAll(“a”, “”).length(); System.out.println(“字串中字元a有”+n+“個”); 原文: https://zhidao.baidu.com/question/1899190409868380700.html 2.出現的所有索引位置: int a = str.indexOf(key);//*第一個出現的索引位置 while (a != -1) { System.out.print(a + “\t”); a = str.indexOf(key, a + 1);//*從這個索引往後開始第一個出現的位置 }