#counting #character #chars #str #counter

chars_counter

实现 &str 类型字符计数的特例

2 个版本

0.1.1 2021年12月12日
0.1.0 2021年12月3日

#17 in #chars

MIT/Apache

10KB
144 代码行,不包括注释

Chars Counter

实现 &str 类型字符计数的特例。

快速开始

use chars_counter::{ICharsCounter, ICharCounterExt};

let str = "Hello world!";
let result = str.count_chars();
// result = [CharsCounter { character: 'l', count: 3 }, CharsCounter { character: 'o', count: 2 }, CharsCounter { character: ' ', count: 1 }, CharsCounter { character: '!', count: 1 }, CharsCounter { character: 'H', count: 1 }, CharsCounter { character: 'd', count: 1 }, CharsCounter { character: 'e', count: 1 }, CharsCounter { character: 'r', count: 1 }, CharsCounter { character: 'w', count: 1 }]

// You can also use like this:
let result = str.count_chars_numeric();
let result = str.count_chars_alphabetic();
let result = str.count_chars_chinese();
// ...... Others you can try by yourself.
// if those can't meet your needs, you can custom your own rules by
let result = str.count_chars_filter(|x| *x != ' '); // ignore whitespaces.

// More features:
let result = str.count_chars().most_chars();
// result = [CharsCounter { character: 'l', count: 3 }]
let result = str.count_chars().least_chars();
// result = [CharsCounter { character: ' ', count: 1 }, CharsCounter { character: '!', count: 1 }, CharsCounter { character: 'H', count: 1 }, CharsCounter { character: 'd', count: 1 }, CharsCounter { character: 'e', count: 1 }, CharsCounter { character: 'r', count: 1 }, CharsCounter { character: 'w', count: 1 }]
let result = str.count_chars().find_by_char('l');
// result = Some(CharsCounter { character: 'l', count: 3 })
let result = str.count_chars().find_by_num(2);
// result = [CharsCounter { character: 'o', count: 2 }]
let result = str.count_chars().least_chars().find_by_char('H');
// result = Some(CharsCounter { character: 'H', count: 1 })

依赖

~425KB