String Count CharactersUse a for-loop to count characters in a string, handling sequential whitespace characters together for more accuracy.
This page was last reviewed on Mar 6, 2022.
Count characters. Suppose you have a Rust str, and you want to count its characters like Microsoft Word. If we treat each space as a separate character, results are incorrect.
Method notes. In word processors, the logic counts sequential whitespace (like space chars) as single characters. This gives a more real-world estimate of character counts.
Example code. To begin, we introduce 2 Rust functions. The count_chars function is more interesting: it has logic to test for sequential whitespace.
Info In count_chars, we use is_whitespace() to detect space chars. Then on each char, we record whether we encountered a whitespace.
Loop, String Chars
And If the previous char was a whitespace char, and the current also is one, we do not count the current one as a character.
Finally The count_nonspace_chars is simpler—it ignores all characters that are whitespace.
fn count_chars(test: &str) -> i32 { let mut result = 0; let mut last_was_space = false; for c in test.chars() { // Only count whitespace chars that are not preceded by another whitespace. if c.is_whitespace() { if last_was_space == false { result += 1; } last_was_space = true; } else { result += 1; last_was_space = false; } } return result; } fn count_nonspace_chars(test: &str) -> i32 { let mut result = 0; for c in test.chars() { // Count all chars that are not whitespace. if !c.is_whitespace() { result += 1; } } return result; } fn main() { let test = "There is a cat, my friend. How are you?"; // Test the functions. let result1 = count_chars(test); let result2 = count_nonspace_chars(test); println!("RESULT1: {}", result1); println!("RESULT2: {}", result2); }
Results note. In my previous research, I found that word processor signore sequential whitespace. And the Rust program results match results of existing methods.
Tip Search for the C# Count Characters method on this site for a complete analysis of word processors.
A summary. It is possible to build functions in Rust that parallel existing software features. Having methods that can give results like this is useful.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 6, 2022 (edit).
© 2007-2024 Sam Allen.