mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 17:29:31 +08:00
test: CountCharTest
(#5423)
test: CountCharTest Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
@ -5,13 +5,16 @@ public final class CountChar {
|
||||
}
|
||||
|
||||
/**
|
||||
* Count non space character in string
|
||||
* Counts the number of non-whitespace characters in the given string.
|
||||
*
|
||||
* @param str String to count the characters
|
||||
* @return number of character in the specified 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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user