【Rust】字符串切片(三)

环境

  • Time 2022-03-24
  • Rust 1.59.0

示例

lines

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

encode_utf16

fn main() {
    let name = "JiangBo";
    println!("{:?}", name.encode_utf16().count());
}

contains

fn main() {
    let name = "JiangBo";
    println!("{:?}", name.contains("ngB"));
}

starts_with

fn main() {
    let name = "JiangBo";
    println!("{:?}", name.starts_with("Ji"));
}

ends_with

fn main() {
    let name = "JiangBo";
    println!("{:?}", name.ends_with("Bo"));
}

find

fn main() {
    let name = "JiangBo";
    println!("{:?}", name.find("Bo"));
}

rfind

fn main() {
    let name = "JiangBo";
    println!("{:?}", name.rfind("Bo"));
}

split

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

split_inclusive

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

总结

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

附录

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