【Rust】字符串切片(四)

环境

  • Time 2022-03-24
  • Rust 1.59.0

示例

rsplit

fn main() {
    let name = "Jiang\nBo";
    name.rsplit('\n').for_each(|e| println!("{e:?}"));
}

split_terminator

fn main() {
    let name = "\nJiang\nBo\n";
    name.split_terminator('\n').for_each(|e| println!("{e:?}"));
}

rsplit_terminator

fn main() {
    let name = "\nJiang\nBo\n";
    name.rsplit_terminator('\n').for_each(|e| println!("{e:?}"));
}

splitn

fn main() {
    let name = "\nJiang\nBo\n";
    name.splitn(3, '\n').for_each(|e| println!("{e:?}"));
}

rsplitn

fn main() {
    let name = "\nJiang\nBo\n";
    name.rsplitn(3, '\n').for_each(|e| println!("{e:?}"));
}

split_once

fn main() {
    let name = "\nJiang\nBo\n";
    println!("{:?}", name.split_once('\n'));
}

rsplit_once

fn main() {
    let name = "\nJiang\nBo\n";
    println!("{:?}", name.rsplit_once('\n'));
}

matches

fn main() {
    let name = "\nJiang\nBo\n";
    name.matches(char::is_alphabetic)
        .for_each(|e| println!("{e:?}"));
}

rmatches

fn main() {
    let name = "\nJiang\nBo\n";
    name.rmatches(char::is_alphabetic)
        .for_each(|e| println!("{e:?}"));
}

总结

了解了字符串切片中相关的一些方法。

附录

posted @ 2022-05-29 07:42  jiangbo4444  阅读(105)  评论(0编辑  收藏  举报