Files
Java/src/main/java/com/thealgorithms/others/CountChar.java
Alex Klymenko 0733075498 test: CountCharTest (#5423)
test: CountCharTest

Co-authored-by: alxkm <alx@alx.com>
2024-08-28 18:45:23 +02:00

21 lines
562 B
Java

package com.thealgorithms.others;
public final class CountChar {
private CountChar() {
}
/**
* Counts the number of non-whitespace characters in the given string.
*
* @param str the input string to count the characters in
* @return the number of non-whitespace characters in the specified string;
* returns 0 if the input string is null
*/
public static int countCharacters(String str) {
if (str == null) {
return 0;
}
return str.replaceAll("\\s", "").length();
}
}