Here you can find the source of countChar(String str, char reg)
public static int countChar(String str, char reg)
//package com.java2s; /*/*from w w w . j ava 2 s. c o m*/ * Copyright (C) 2010 Viettel Telecom. All rights reserved. * VIETTEL PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ public class Main { public static int countChar(String str, char reg) { char[] ch = str.toCharArray(); int count = 0; for (int i = 0; i < ch.length; ++i) { if (ch[i] == reg) { if (ch[i + 1] == reg) { ++i; continue; } ++count; } } return count; } }