Skip to content

字符串

Rust中字符用的是Unicode编码,字符串用UTF-8编码。

创建新字符串

rust
// 将utf8转换为字符串
let data = [240, 159, 146, 150];         // 💖
let s = String::from_utf8(data.to_vec()).unwrap();
println!("{}", s);
rust
// 将utf16转换为字符串
let v = vec![0x0048, 0x0065, 0x006C, 0x006C, 0x006F];
println!("{}", String::from_utf16(&v).unwrap());
rust
// 尽可能的转换成字符串
let data = [240, 159, 146, 150];
let s = String::from_utf8_lossy(&data);
println!("{}", s);  // 💖

let bytes = b"Hello, \xF0\x90\x80World";
// 尽可能的转换成字符串
let string = String::from_utf8_lossy(bytes);
println!("{}", string);     // 💖

// 转换成字符串,如果遇到非法字符,则返回错误
// Err(FromUtf8Error { bytes: [72, 101, 108, 108, 111, 44, 32, 240, 144, 128, 87, 111, 114, 108, 100], error: Utf8Error { valid_up_to: 7, error_len: Some(3) } })
let string = String::from_utf8(bytes.to_vec());
println!("{:?}", string);           // Err(...)

修改字符串

rust
// 插入字符
let mut s = String::from("");
s.insert(0, 'a');
s.insert(1, 'b');
assert_eq!(s, "ab");
println!("{}", s);      // ab
rust
// 插入字符串
let mut s = String::from("lucky");
s.insert_str(0, "good ");
assert_eq!(s, "good lucky");
println!("{}", s);      // good lucky
rust
// 追加字符
let mut s = String::from("hello");
s.push(' ');
s.push('w');
s.push('o');
s.push('r');
s.push('l');
s.push('d');
assert_eq!(s, "hello world");
println!("{}", s);      // hello world
rust
// 追加字符串
let mut s = String::from("hello");
s.push_str(" world");
assert_eq!(s, "hello world");
println!("{}", s);      // hello world

字符串转换

rust
// 将字符串转换为字节数组
let s = String::from("hello");
let bytes = s.as_bytes();
assert_eq!(bytes, b"hello");
println!("{:?}", bytes);        // [104, 101, 108, 108, 111]
rust
// 将字符串转换为字节数组
let s = String::from("hello");
let bytes = s.into_bytes();
assert_eq!(bytes, b"hello".to_vec());
println!("{:?}", bytes);  // [104, 101, 108, 108, 111]
rust
// 将字符串转换为可变字符串
let mut s = String::from("hello");
let s_mut_str = s.as_mut_str();
s_mut_str.make_ascii_uppercase();
assert_eq!(s, "HELLO");
println!("{}", s);
rust
// 提取字符串切片
let s = String::from("hello");
let slice = s.as_str();
assert_eq!(slice, "hello");
println!("{}", slice);

删除字符串

rust
// 删除字符串中的字符
let mut s = String::from("hello");
let c = s.remove(0);
assert_eq!(c, 'h');
assert_eq!(s, "ello");
println!("{}", s);      // ello
rust
// 截断字符串
let mut s = String::from("hello world");
s.truncate(5);
assert_eq!(s, "hello");
println!("{}", s);      // hello
rust
// 删除字符串中的最后一个字符
let mut s = String::from("hello world");
let c = s.pop();
assert_eq!(c, Some('d'));
assert_eq!(s, "hello worl");
println!("{}", s);      // hello worl
rust
// 清空字符串
let mut s = String::from("hello");
s.clear();
assert_eq!(s.len(), 0);
assert!(s.is_empty());
assert_eq!(s.capacity(), 5);   // 重点:虽然清空了,但是容量还是5
println!("{}", s);
rust
// 删除部分字符串
let mut s = String::from("hello world");
let o1 = s.find("w").unwrap_or(s.len());
println!("offset:{}", o1);

// 删除 world
let t:String = s.drain(..o1).collect();
assert_eq!(t, "hello ");
assert_eq!(s, "world");

// 清空全部
s.drain(..);            // 删除全部和clear类似
assert_eq!(s, "");

替换字符串

rust
// 替换字符串
let s = String::from("hello world");
let s1 = s.replace("world", "rust");
assert_eq!(s1, "hello rust");
println!("{}", s1);     // hello rust
rust
// 替换字符串范围
let mut s = String::from("hello world");
s.replace_range(6..11, "rust");
assert_eq!(s, "hello rust");
println!("{}", s);      // hello rust

分割字符串

rust
// 分割字符串
let s = String::from("hello world");
for word in s.split(' ') {
    println!("{}", word);
}
rust
// 分割空白字符 (如果存在多个空白符,只保留一个)
let s = String::from("hello   \n world");
for word in s.split_whitespace() {
    println!("{}", word);
}
// hello
// world
rust
// 分割字符串,并返回分割的位置
let mut s = String::from("hello world");
let s1 = s.split_off(5);
assert_eq!(s, "hello");
assert_eq!(s1, " world");
println!("{}", s);      // hello
println!("{}", s1);

容量相关

rust
let empty_string = String::new();
println!("length: {}", empty_string.len());

let data = "abc";
println!("{} length: {}", data, data.len());

let data = "中国人";
println!("{} length: {}", data, data.len());

let data = "🐶";
println!("{} length: {}", data, data.len());

// length: 0 
// abc length: 3
// 中国人 length: 9
// 🐶 length: 4
rust
// 获取字符串的缓冲区大小
let s = String::with_capacity(10);
assert_eq!(s.capacity(), 10);
println!("{}", s.capacity());       // 10
rust
// 预留空间,减少分配次数
let mut s = String::new();
s.reserve(10);  // 增加10个字节
assert_eq!(s.capacity(), 10);
println!("{}", s.capacity());
s.push('a');
assert_eq!(s.capacity(), 10);
println!("{}", s.capacity());
rust
// 减少字符串的容量,以适应当前字符串的长度
let mut s = String::from("hello");
s.reserve(100);             // 增加字符串的容量
assert_eq!(s.capacity(), 105);
println!("{}", s.capacity());       // 105
s.shrink_to_fit();          // 减少字符串的容量
assert_eq!(s.capacity(), 5);
println!("{}", s.capacity());       // 5
rust
// 减少字符串的容量,以适应指定长度
let mut s = String::from("hello");
s.reserve(100);
assert_eq!(s.capacity(), 105);
println!("{}", s.capacity());       // 105
s.shrink_to(10);
assert_eq!(s.capacity(), 10);
println!("{}", s.capacity());       // 10

人生感悟