Rust 标准库 API 字符串 String &str
Rust About 3,008 words创建 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);
Views: 1,311 · Posted: 2023-04-16
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...