Counting characters in Java

There are many ways for counting the number of characters in a String.

Below a simple/naive approach:

public class CountChars {

	public static void main(String[] args) {
		System.out.println(count("He-Man is a fictional superhero"));

    private static int count(String text) {
        int count = 0;
        for (int i = 0; i < text.length(); i++) {
        return count;


Leave a Reply

Your email address will not be published. Required fields are marked *