Count Characters in a String in Java
-
Use
String.length()
to Count Total Characters in a Java String - Use Java 8 Stream to Count Characters in a Java String
-
Use Loop and
charAt()
to Count a Specific Character in a Java String

Today, we will introduce multiple ways to count the characters in a given Java string. We will count the total characters and the specific characters.
Use String.length()
to Count Total Characters in a Java String
The most common practice to get the total count of characters in a Java string is to use the length()
method. In the below code, we have a string exampleString
and will use exampleString.length()
to get this string’s total length.
The output shows that there are 28 characters in exampleString
while there are only 23 characters. It happens because String.length()
counts the whitespaces too. To tackle this problem, we can use the replace()
function to replace all the whitespaces with an empty character that is not counted. Finally, we can get the length of the string without any whitespaces, which is 23.
public class CountCharsInString {
public static void main(String[] args) {
String exampleString = "This is just a sample string";
int stringLength = exampleString.length();
System.out.println("String length: " + stringLength);
int stringLengthWithoutSpaces = exampleString.replace(" ", "").length();
System.out.println("String length without counting whitespaces: " + stringLengthWithoutSpaces);
}
}
Output:
String length: 28
String length without counting whitespaces: 23
Use Java 8 Stream to Count Characters in a Java String
Another way to count all the characters in a string is to use the String.chars().count()
method that returns the total number of characters in the string, but including whitespaces. As chars()
is a stream, we can use the filter()
method to ignore the whitespaces. filter(ch -> ch != ' ')
checks every character and if it founds a whitespace it will filter it out.
public class CountCharsInString {
public static void main(String[] args) {
String exampleString = "This is just a sample string";
long totalCharacters = exampleString.chars().filter(ch -> ch != ' ').count();
System.out.println("There are total " + totalCharacters + " characters in exampleString");
}
}
Output:
There are total 23 characters in exampleString
Use Loop and charAt()
to Count a Specific Character in a Java String
We have been counting the total characters in a string, but the below example shows the method to count a specific character in the string. Our goal is to get the number of i
in exampleString
. To achieve this, we used a loop that runs until the string’s end.
We create two additional variables: totalCharacters
that will hold the count, and temp
that will hold every individual character using exampleString.charAt(i)
. To check our character’s occurrence, we will compare temp
with our character to check if they match. If it finds a match, then totalCharacters
will be incremented by one. Once the loop is over, we can see the total occurrences of our character in the string.
public class CountCharsInString {
public static void main(String[] args) {
String exampleString = "This is just a sample string";
int totalCharacters = 0;
char temp;
for (int i = 0; i < exampleString.length(); i++) {
temp = exampleString.charAt(i);
if (temp == 'i')
totalCharacters++;
}
System.out.println("i appears " + totalCharacters + " times in exampleString");
}
}
Output:
i appears 3 times in exampleString
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedInRelated Article - Java String
- Perform String to String Array Conversion in Java
- Remove Substring From String in Java
- Convert Byte Array in Hex String in Java
- Convert Java String Into Byte
- Generate Random String in Java
- The Swap Method in Java
Related Article - Java Char
- Convert Int to Char in Java
- Char vs String in Java
- Initialize Char in Java
- Represent Empty Char in Java
- Char to Uppercase/Lowercase in Java
- Check if a Character Is Alphanumeric in Java