微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将 UTF-8 字符串拆分成块

如何解决将 UTF-8 字符串拆分成块

我想将一个 UTF-8 字符串分成大小相等的块。我想出了一个解决方案来做到这一点。现在我想简化它,如果可能的话,删除一个对方付费电话。有办法吗?

fn main() {
    let strings = "ĄĆĘŁŃÓŚĆŹŻ"
        .chars()
        .collect::<Vec<char>>()
        .chunks(3)
        .map(|chunk| chunk.iter().collect::<String>())
        .collect::<Vec<String>>();
    println!("{:?}",strings);
}

Playground link

解决方法

您可以使用 chunks() from Itertools

use itertools::Itertools; // 0.10.1

fn main() {
    let strings = "ĄĆĘŁŃÓŚĆŹŻ"
        .chars()
        .chunks(3)
        .into_iter()
        .map(|chunk| chunk.collect::<String>())
        .collect::<Vec<String>>();
    println!("{:?}",strings);
}
,

这不需要 Itertools 作为依赖项,也不分配,因为它遍历原始字符串的切片:

fn chunks(s: &str,length: usize) -> impl Iterator<Item=&str> {
    assert!(length > 0);
    let mut indices = s.char_indices().map(|(idx,_)| idx).peekable();
    
    std::iter::from_fn(move || {
        let start_idx = match indices.next() {
            Some(idx) => idx,None => return None,};
        for _ in 0..length - 1 {
            indices.next();
        }
        let end_idx = match indices.peek() {
            Some(idx) => *idx,None => s.bytes().len(),};
        Some(&s[start_idx..end_idx])
    })
}


fn main() {
    let strings = chunks("ĄĆĘŁŃÓŚĆŹŻ",3).collect::<Vec<&str>>();
    println!("{:?}",strings);
}
,

考虑到字素的问题后,我最终得到了以下解决方案。

我使用了 unicode-segmentation 板条箱。

use unicode_segmentation::UnicodeSegmentation;                                                                                                                            

fn main() {
    let strings = "ĄĆĘŁŃÓŚĆŹŻèèèèè"
        .graphemes(true)                                                                                                                                          
        .collect::<Vec<&str>>()                                                                                                                                   
        .chunks(length)                                                                                                                                           
        .map(|chunk| chunk.concat())                                                                                                                              
        .collect::<Vec<String>>();
    println!("{:?}",strings);
}

我希望仍然可以进行一些简化。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。