計算字符串String中指定字符數量

/**
 * 統計字符串中“-”的數量
 * @param str 要統計的字符
 * @return
 */
public int levelCount(String str){
	int count = 0;
    int start = 0;
    while (str.indexOf("-", start) >= 0 && start < str.length()) {
      count++;
      start = str.indexOf("-", start) + "-".length();
    }
    return count;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章