. Advertisement .
..3..
. Advertisement .
..4..
There are several ways to count characters in a string in Java, using built-in or external libraries. This guide will introduce the simplest solutions you should start with.
Count Characters In A String In Java
With String.length()
The String class in Java comes with the length()
method. It returns the total number of characters in a given string. Keep in mind that this method counts every character, including white spaces and special characters, not just letters and characters. An integer value is returned, indicating the length of that string.
Example:
public class MyClass {
public static void main(String args[]){
String str = "Welcome to ITTutoria.net!";
System.out.println(
"The string has "+ str.length() + " characters.");
}}
Output:
The string has 25 characters.
As you can see, the length()
method is quite straightforward to use. You can insert it directly into a console message. But there are many more possibilities with this method. You can combine its returned value to accomplish more complicated tasks in Java.
For instance, this is how you can check whether a string violates a maximum length requirement:
import java.util.Scanner;
public class MyClass {
public static void main(String args[]){
Scanner myObj = new Scanner(System.in);
System.out.println("Enter your string: ");
String userStr = myObj.nextLine();
int len = userStr.length();
if (len >= 10) {
System.out.println(
"Your string should have fewer than 10 characters.");
}
else {
System.out.println(
"Great! The length of your string is: " + len);
}
}
}
Output:
Enter your string:
ITTutoria
Great! The length of your string is: 9
In many situations, you may not want to take leading and trailing whitespace when counting characters in a string. You can use the trim()
method in conjunction with length()
to remove the whitespace padding around two ends of the string.
Example:
public class MyClass {
public static void main(String args[]){
String str = " Welcome to ITTutoria.net! ";
System.out.println("The length of string (without trimming): " + str.length());
System.out.println("The length of string (with trimming): " + str.trim().length());
}
}
Output:
The length of string (without trimming): 30
The length of string (with trimming): 25
The program above shows the difference when you do and don’t count whitespace padding in a string.
With String.charAt()
Sometimes we only need to count the occurrences of just a character, not every character in a string. You can use the charAt()
method for this task. Its syntax:
public char charAt(int index)
It returns the character at the index of a string (note that Java uses zero-based indexing). Combine this value with a for loop, and you can count any character in a string.
Example:
public class MyClass {
public static void main(String args[]){
String myStr = "ITTtutoria";
char myChar = 'T';
int count = 0;
for (int i = 0; i < myStr.length(); i++) {
if (myStr.charAt(i) == myChar) {
count++;
}
}
System.out.println(count);
}
}
Output:
2
The output indicates that there are two "T"
in the given string. This means that you must take letter cases into consideration when using the method charAt()
. This can become a deadly mistake if you forget to check the cases you want to count.
Conclusion
You can use the methods length()
and charAt()
to count characters in a string in Java, depending on whether you can to counter every or just one character. The String class also has several other methods, which you can learn more about here, for example, to remove a character from a string.
Leave a comment