I would like to count the occurrences of a character in a string, suppose I have the string "aaaab", how would i count the amount of a's in it?
-
8It looks like you tagged this question with `regex`. Remember, when confronted with a problem some people think "Hey, I'll use a regular expression!" Now they have two problems. – Greg Hewgill Sep 21 '10 at 20:04
-
@Greg That's only a problem when people use regex [inappropriately](http://betterwaytomakealiving.com/_wordpress/wp-content/uploads/2010/05/square-peg-round-hole.jpg) (like in this question) – NullUserException Sep 21 '10 at 20:07
-
Well any solution would be fine, but i am interested in seeing one in regex too. – Steffan Harris Sep 21 '10 at 20:22
-
Same as http://stackoverflow.com/questions/275944/how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string – Jonik Aug 12 '13 at 16:57
16 Answers
Guava's CharMatcher API is quite powerful and concise:
CharMatcher.is('a').countIn("aaaab"); //returns 4

- 266,786
- 75
- 396
- 414
String string = "aaab";
int count = string.length() - string.replaceAll("a", "").length();
instead of "a" use a regex like "[a-zA-Z]" to count all word characters

- 221
- 2
- 2
Try using Apache Commons' StringUtils:
int count = StringUtils.countMatches("aaaab", "a");
// count = 4

- 4,713
- 2
- 28
- 31
-
Note that StringUtils will find occurrences of a String within another String, so might not be as efficient as using a character-specific search. – dty Sep 21 '10 at 20:05
-
2
-
@MikeG, please update link: http://commons.apache.org/proper/commons-lang//apidocs/org/apache/commons/lang3/StringUtils.html – Yura Shinkarev Apr 20 '13 at 20:14
The code looks way easier to read if you don't use regular expressions.
int count = 0;
for(int i =0; i < string.length(); i++)
if(string.charAt(i) == 'a')
count++;
count
now contains the number of 'a's in your string. And, this performs in optimal time.
Regular expressions are nice for pattern matching. But just a regular loop will get the job done here.

- 136,852
- 53
- 295
- 323
-
'jjnguy' Nelson: your (accepted) answer only works if you plan on counting Java char. It doesn't work for all the Unicode characters that a Java String can contain. String's *codePointAt(...)* is the method you're looking for, not *charAt(...)*, which is broken since Unicode 3.1 came out. – SyntaxT3rr0r Sep 22 '10 at 01:38
-
@Web could you please point me to a reference? I'd be interested in learning more. – jjnguy Sep 22 '10 at 02:14
-
'jjnguy' Nelson: I think the JavaDoc are exhaustive (not sure that said). Basically, *charAt* returns 16 bits value and since Unicode 3.1 / Java 1.5 there are more than 65536 characters supported by Unicode (and Java). Hence *charAt* can return "something" that is not a Unicode character. The newer *codePointAt* returns a 32 bit value and can hence contain all valid Unicode characters. – SyntaxT3rr0r Sep 22 '10 at 14:02
-
1@Web, ok. That makes sense. I though 16bits was enough... I will leave my answer the way it is though. Adding that unfamiliar method would not be helpful to people new to the language. And, your comment right below serves to point out the flaw in the code. – jjnguy Sep 22 '10 at 14:38
Regular expressions aren't particularly good at counting simple things. Think ant+sledgehammer. They are good at busting complex strings up into pieces.
Anyway, here's one solution the OP is interested in - using a regex to count 'a's:
public class Reggie {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("[^a]*a");
Matcher matcher = pattern.matcher("aaabbbaaabbabababaaabbbbba");
int count = 0;
while(matcher.find()) {
count++;
}
System.out.println(count+" matches");
}
}
This is a pretty slow way to do it, as pointed out by others. Worse, it isn't the easiest and certainly isn't the most likely to be bug-free. Be that as it may, if you wanted something a little more complex than 'a' then the regex would become more appropriate as the requested string got more complex. For example, if you wanted to pick dollar amounts out of a long string then a regex could be the best answer.
Now, about the regex: [^a]*a
This [^a]*
means 'match zero or more non-'a' characters. This allows us to devour non-'a' crud from the beginning of a string: If the input is 'bbba' then [^a]*
will match 'bbb'. It doesn't match the 'a'. Not to worry, the trailing 'a' in the regex says, "match exactly one 'a'". So our regex says, "match zero or more non-'a' characters that are followed by an 'a'."
Ok. Now you can read about Pattern and Matcher. The nutshell is that the Pattern is a compiled regular expression. It is expensive to compile a regex so I make mine static so they only get compiled once. The Matcher is a class that will apply a string to a Pattern to see if it matches. Matcher has state information that lets it crawl down a string applying a Pattern repeatedly.
The loop basically says, "matcher, crawl down the string finding me the next occurrence of the pattern. If we find it, increment the counter." Note the character sequences being found by Matcher isn't just 'a'. It is finding sequences like the following: 'a', 'bbba', 'bba', 'ba', etc. That is, strings that don't contain an 'a' except for their last character.

- 73,866
- 12
- 100
- 156

- 12,000
- 7
- 52
- 73
int count = 0;
for (char c : string.toCharArray())
if (c == 'a')
count++;

- 23,354
- 24
- 59
- 84
-
-
-
Converting the String to a char[] will allocate a new char[] which will be discarded as soon as the loop is finished. – dty Sep 21 '10 at 20:11
-
1@dty But the GC will take care of it. Unless your string is huge, I don't think this is a big deal. – Aillyn Sep 21 '10 at 20:12
-
On the contrary. For my day job, I work on ultra-low latency systems, and we have to be completely anal about the amount of garbage we generate in order to get maximum performance. – dty Sep 21 '10 at 20:17
-
@dty, ultra-low latency systems in Java, I see. Anyway, it's getting *waaaay* past my bedtime and now is a good time to leave, I guess :) – Bart Kiers Sep 21 '10 at 20:20
-
2I continue to be floored at the number of people who write on SO about using Java for low latency systems. Its like using Assembler for cross platform development - sometimes you are just using the wrong tool for the job. – Yishai Sep 21 '10 at 20:23
-
2Wrong tool how? I can't go into specifics, but we can get tens of thousands of messages from our border, through our proprietary reliable middleware and several server hops, and back out to the border with single millisecond latencies, consistently and without significant latency spikes, using commodity hardware and a SINGLE THREADED architecture which includes complete hot/hot failover and journaling. How exactly is that the wrong tool? – dty Sep 21 '10 at 21:02
-
3I continue to be floored by people who say Java can't be used to write high performance systems just because they aren't capable of writing high performance code! :-) – dty Sep 21 '10 at 21:03
-
@dty, you're right: Java can surely be used for high performance systems. But in this case (counting the occurrences of characters in a string) talking about the fact that `toCharArray()` is inefficient makes little sense to me. If the OP had mentioned s/he was operating on extremely large strings, then I would understand it, but not now. It is obvious that this is just an exercise. – Bart Kiers Sep 22 '10 at 06:47
-
Absolutely. That's why I made a positive comment about the answer, and didn't down-rank it! :-) – dty Sep 22 '10 at 11:31
String searchFor = "a";
String base = "aaaab";
int count=0;
int index =base.indexOf(searchFor);
while(index!=-1){
++count;
index = base.indexOf(searchFor, index+searchFor.length());
}
System.out.println(count);

- 488
- 2
- 7
- 21
A simple loop over the characters would do it.
public int countChars(char c, String s) {
int result = 0;
for (int i = 0, n = s.length(); i < n; i++) {
if (s.charAt(i) == c) {
result++;
}
}
return result;
}

- 18,795
- 6
- 56
- 82
-
FYI: any decent JRE's JIT will move the `i < s.length()` from `for (int i = 0; i < s.length(); i++)` for you: there is often no need to make code harder to read by such "optimizations". Here's a nice article about "clever" programming tricks: [Write Dumb Code -- Advice From Four Leading Java Developers](http://java.sun.com/developer/technicalArticles/Interviews/devinsight_1/) – Bart Kiers Sep 21 '10 at 20:31
-
As a pattern, this prevents you from thinking about whether the limiting expression is something that the compiler can optimise/is constant. For example, writing it this way, saves me from having to think about whether `for (int i = 0; i < expensiveCalculation(); i++)...` really is expensive and/or constant and/or can be hoisted out of the loop. – dty Sep 21 '10 at 20:59
-
Here is a really short solution without any extra libraries:
String input = "aaaab";
int i = -1, count = 0;
while( (i = input.indexOf( 'a', i + 1 ) ) != -1 ) count++;
System.out.println( count );

- 39,095
- 19
- 120
- 139
public static void main(String[] args) {
Map<Character, Integer> data = new HashMap<Character, Integer>();
String s = "aaaab";
char[] chars = s.toCharArray();
for (char a : chars) {
if (data.containsKey(a)) {
int value = data.get(a);
data.put(a, value + 1);
} else {
data.put(a, 1);
}
}
Iterator it = data.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}
}

- 598
- 1
- 5
- 17
You can simply use this:
String a = "i am here as junior java programmer";
Set temp = new HashSet();
char[] chararray=a.toCharArray();
Set temp=new HashSet();
for(int i=0;i<chararray.length;i++)
{
int count=0;
for (int j=0;j<chararray.length;j++) {
if (chararray[i]==chararray[j]) {
count++;
}
}
if (temp.add(chararray[i])!=false)
System.out.println("Character "+chararray[i]+" occur "+count);
}

- 28,765
- 10
- 55
- 103

- 11
- 1
-
1This seems unnecessarily complex. A single loop over the characters should do it. Also, this question is quite old. Best not to revive old threads unless the response adds a significant improvement to previous answers. – Leigh Jun 16 '12 at 01:30
String s1="parasanna";
StringBuffer sb=new StringBuffer();
boolean print = false;
for (int i=0; i<s1.length(); i++){
int count=1;
char c=s1.charAt(i);
sb.append(c);
for (int j=1; j<sb.length(); j++) {
char c2=sb.charAt(j-1);
if (c==c2) {
count++;
}
}
System.out.println(c+"=="+count);
}

- 3,401
- 13
- 31
- 46

- 11
- 1
Here is My Logic ...
public class OccurenceOf_Character {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println(" Enter a string");
String str = input.nextLine();
System.out.println(" Enter a character");
String character=input.next();
int l = character.length();
char c=character.charAt(0);
int count=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i) == c)
{
count=count+1;
}
}
System.out.println(count);
}
}

- 7,401
- 4
- 52
- 58

- 11
- 2
You could use StringUtils class provided by apache commons. StringUtils.countMatches(String originalString, String subCharacterSequesnce)

- 11
- 1
-
This has already been [suggested](http://stackoverflow.com/a/3764010/20938). – Alan Moore Feb 28 '15 at 07:44
Java 8
Approach 1 - Gets the occurrence of a single character
String sentence = "Aaron ate apples upon a rock";
long counted = IntStream.range(0, sentence.length())
.filter(i->sentence.charAt(i) == 'a')
.count();
System.out.println("First approach: " + counted);
Approach 2 - Allows the character to be specified
String sentence = "Aaron ate apples upon a rock";
BiFunction<String, Character, Long> counter = (s,c) -> {
return IntStream.range(0, s.length())
.filter(i->s.charAt(i) == c)
.count();
};
System.out.println("Second approach (with 'a'): " + counter.apply(sentence, 'a'));
System.out.println("Second approach (with 'o'): " + counter.apply(sentence, 'o'));
Approach 3 - Counts occurrences of all characters
String sentence = "Aaron ate apples upon a rock";
Map<Character, Long> counts = IntStream.range(0, sentence.length())
.mapToObj(i->sentence.charAt(i))
.collect(Collectors.groupingBy(o->o, Collectors.counting()));
System.out.println("Third approach for every character... ");
counts.keySet().stream()
.forEach(key -> System.out.println("'" + key + "'->" + counts.get(key)));

- 1,993
- 1
- 15
- 11
public static void main(String[] args) throws IOException
{
//String s1="parasanna";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter string:");
String s1 = br.readLine();
StringBuffer sb=new StringBuffer(s1);
while(sb.length() != 0)
{
char c = sb.charAt(0);
int cnt = 0;
for(int i=0; i< sb.length(); i++)
{
if(c == sb.charAt(i))
{
cnt++;
sb.deleteCharAt(i);
i--;
}
}
System.out.println(c + " occurance is:" + cnt);
}
}