其他分享
首页 > 其他分享> > 【Rust】字符串切片(三)

【Rust】字符串切片(三)

作者:互联网

环境

示例

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:?}"));
}

总结

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

附录

标签:name,JiangBo,切片,println,let,fn,字符串,main,Rust
来源: https://www.cnblogs.com/jiangbo4444/p/16322801.html