Explain Codes LogoExplain Codes Logo

How do I count the number of occurrences of a char in a String?

java
prompt-engineering
functions
performance
Anton ShumikhinbyAnton Shumikhin·Oct 8, 2024
TLDR

To swiftly count a char's frequency in a String apply the length() and replace() methods:

int countChar(String s, char c) { return s.length() - s.replace("" + c, "").length(); }

Here's how you check 'e' occurrences in "hello":

System.out.println(countChar("hello", 'e')); // Output: 1

This approach measures the difference in length before and after removing the target character. It's quite clever, don't you think?

Practical variations

While the basic method is simple, real-world scenarios often require more adaptive techniques.

Streamlined approach with Java 8 Streams

Leverage Java 8 Streams for a clean, functional style:

long count = "hello".chars().filter(ch -> ch == 'e').count(); System.out.println(count); // Output: 1

This filters the character stream from the string, matching the targeted character, then dishing out the count. Sleek, huh?

Including all Unicode characters

For Unicode characters that are beyond the BMP (Basic Multilingual Plane), use codePoints():

long countUnicode = "hello 😊".codePoints().filter(ch -> ch == '😊').count(); System.out.println(countUnicode); // Output: 1

Emojis don't scare us! This takes goofy emojis or complex characters in stride.

Exploiting efficient libraries

Apache Commons Lang or Spring provide concise solutions. Let's use Apache Commons Lang:

int count = StringUtils.countMatches("hello", "e"); System.out.println(count); // Output: 1

Trust these libraries: they've been there, done that, got the t-shirt. Plus, they might be faster!

Juggling with regex

Regex is a magician's trick in your toolkit. To count periods:

int countDots = "a.b.c.d".replaceAll("[^.]", "").length(); System.out.println(countDots); // Output: 3

But remember, great power comes with great responsibility! Regexes can slow you down in simple tasks.

Dodging pitfalls

When using split(), be aware of traps. Adjacent separators can be a bit sneaky:

// A wild pitfall appears with split() int countSplit = "a..b...c....d".split("\\.", -1).length - 1; System.out.println(countSplit); // Output: 9. Hold on, that's not quite right...

The -1 limit is like a safety net – it catches trailing empty strings that could be left out of the count.

Benchmarking your solutions

Performance is like a high-speed car race, with methods, inputs, and JVM versions as your competitors. Keep JMH benchmarks as your pitstop engineer to gauge the fastest solution.

Customizing to your needs

Tailor your coat according to the cloth. In code, that means:

  • For better comprehension: Pack the counting logic into a neat little method.
  • For the need for speed: Unroll loops, make micro-optimizations. No need to show off though!
  • For dependable code: Use libraries like a lazy (but smart) developer.