Java OpenResty Spring Spring Boot MySQL Redis MongoDB PostgreSQL Linux Android Nginx 面试 小程序 Arthas JVM AQS juc Kubernetes Docker 诊断工具


Rust 标准库 API 字符串 String &str

Rust 大约 3008 字

创建 String

from

let s = String::from("abc");

new

let mut s = String::new();
s.push_str("aaa");

with_capacity

let mut with_capacity: String = String::with_capacity(10);
with_capacity.push_str("aaa");

String 转为 &str

let as_str: &str = String::from("123").as_str();

let as_mut_str: &mut str = String::from("123").as_mut_str();

&str 转为 String

let ref_str_to_string: String = "123".to_string();

is_empty 判断是否为空

let result: bool = String::from("xyz").is_empty();

let result: bool = "xyz".is_empty();

is_ascii 判断是否是 ASCII 码

let result: bool = String::from("xyz   1").is_ascii();

let result: bool = "xyz我".is_ascii();

parse 解析为其他类型

parse()需在变量上指定。(let result: i32

parse::<i32>()显式指定转换类型。

let result: i32 = String::from("123").parse().unwrap();

let result = String::from("123").parse::<i32>().unwrap();

let result = "1.2".parse::<f32>().unwrap();

len 获取长度

let len: usize = String::from("abc").len();

let len: usize = "123".len();

contains 判断是否包含字符串切片或字符

&str类型和char字符类型都可以。

let result: bool = String::from("aadaa").contains("da");

let result: bool = String::from("aadaa").contains('a');

starts_with/ends_with 判断开始/结尾

&str类型和char字符类型都可以。

let result: bool = String::from("xyz").starts_with("x");

let result: bool = String::from("xyz").starts_with('x');

let result: bool = String::from("xyz").ends_with("z");

let result: bool = "xyz".ends_with('z');

trim 裁剪

trim

let trim: &str = String::from(" abc ").trim();

let trim: &str = " abc ".trim();

trim_start

let trim: &str = " abc ".trim_start();

trim_end

let trim: &str = " abc ".trim_end();

trim_match

trim_matches中不能传&str类型。

let trim: &str = "abc".trim_matches('a');

let string: String = String::from("abc ");
let trim: &str = string.trim_matches(char::is_numeric);

to_uppercase 转为大写

let to_uppercase: String = String::from("abc").to_uppercase();

let to_uppercase: String = "abc".to_uppercase();

to_lowercase 转为小写

let to_lowercase: String = String::from("ABC").to_lowercase();

let to_lowercase: String = "ABC".to_lowercase();

replace 替换

replace(pat, to) 全部替换

let replace: String = String::from("qazwsx").replace("qaz", "zaq");

let replace: String = "qazwsx".replace("qaz", "zaq");

replacen(pat, to, count) 替换指定个数

let replace: String = String::from("qazwsx").replacen("wsx", "xsw", 1);

let replace: String = "qazwsx".replacen("wsx", "xsw", 1);

repeat 重复字符

let repeat = String::from("qazwsx").repeat(3);

let repeat = "qazwsx".repeat(3);

split 分割字符串

split

let string = String::from("abc");
let split: Vec<&str> = string.split("").collect();

let split: Vec<&str> = "abc".split("").collect();

split_ascii_whitespace

按空白字符分割。

let string = String::from("a b c");
let split_ascii_whitespace: Vec<&str> = string.split_ascii_whitespace().collect();

split_once

只分割第一个匹配的。

let string = String::from("abc");
let split_once: (&str, &str) = string.split_once("").unwrap_or_default();

truncate 截断字符串

let mut string = String::from("qazwsx");
string.truncate(5);
阅读 647 · 发布于 2023-04-17

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb

扫描下方二维码关注公众号和小程序↓↓↓

扫描二维码关注我
昵称:
随便看看 换一批