【Rust】字符串切片(五)

环境

  • Time 2022-03-24
  • Rust 1.59.0

示例

match_indices

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

rmatch_indices

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

trim

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

trim_start

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

trim_end

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

trim_matches

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

trim_start_matches

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

trim_end_matches

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

strip_prefix

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

总结

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

附录

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