Skip to content

1.2 字符串

字符串创建

  1. 字面量创建,直接在栈上面创建的
rust
let my_name = "Pascal";
let my_name = "Pascal";
  1. String类型创建,在堆上创建数据,栈上面存储引用
rust
let name = String::from("test")
let name = String::from("test")

字符串截取

  1. 截取区间
rust
let s = String::from("hello world");

let hello = &s[0..5];
let hello = &s[..5];
let world = &s[6..11];
let s = String::from("hello world");

let hello = &s[0..5];
let hello = &s[..5];
let world = &s[6..11];
  1. 截取末尾
rust
let len = s.len();

let slice = &s[4..len];
let slice = &s[4..];
let len = s.len();

let slice = &s[4..len];
let slice = &s[4..];
  1. 截取全部
rust
let slice = &s[..];
let slice = &s[..];

String 与 &str 的转换

rust
fn main() {
  let s = String::from("hello,world!");
  say_hello(&s);
  say_hello(&s[..]);
  say_hello(s.as_str());
}

fn say_hello(s: &str) {
  println!("{}",s);
}
fn main() {
  let s = String::from("hello,world!");
  say_hello(&s);
  say_hello(&s[..]);
  say_hello(s.as_str());
}

fn say_hello(s: &str) {
  println!("{}",s);
}

操作String

  1. push
rust
let mut s = String::from("Hello ");
s.push_str("rust");
let mut s = String::from("Hello ");
s.push_str("rust");
  1. insert
rust
let mut s = String::from("Hello rust!");
s.insert(5, ',');
let mut s = String::from("Hello rust!");
s.insert(5, ',');
  1. replace
rust
let string_replace = String::from("I like rust. Learning rust is my favorite!");

let new_string_replace = string_replace.replace("rust", "RUST");
let string_replace = String::from("I like rust. Learning rust is my favorite!");

let new_string_replace = string_replace.replace("rust", "RUST");
  1. replacen

可以指定替换几次

rust
let string_replace = "I like rust. Learning rust is my favorite!";
let new_string_replacen = string_replace.replacen("rust", "RUST", 1);
let string_replace = "I like rust. Learning rust is my favorite!";
let new_string_replacen = string_replace.replacen("rust", "RUST", 1);
  1. delete
rust
let mut string_pop = String::from("rust pop 中文!");
// 删除最后一个
let p1 = string_pop.pop();

// 删除第一个汉字
string_pop.remove(0);

// 删除字符串中从指定位置开始到结尾的全部字符
string_pop.truncate(3);
let mut string_pop = String::from("rust pop 中文!");
// 删除最后一个
let p1 = string_pop.pop();

// 删除第一个汉字
string_pop.remove(0);

// 删除字符串中从指定位置开始到结尾的全部字符
string_pop.truncate(3);
  1. clear
rust
let mut string_clear = String::from("string clear");
string_clear.clear();
let mut string_clear = String::from("string clear");
string_clear.clear();
  1. concat
rust
let string_append = String::from("hello ");
let string_rust = String::from("rust");
// &string_rust会自动解引用为&str
let result = string_append + &string_rust;
let mut result = result + "!"; // `result + "!"` 中的 `result` 是不可变的
result += "!!!";
let string_append = String::from("hello ");
let string_rust = String::from("rust");
// &string_rust会自动解引用为&str
let result = string_append + &string_rust;
let mut result = result + "!"; // `result + "!"` 中的 `result` 是不可变的
result += "!!!";

使用 + 或者 += 连接字符串,要求右边的参数必须为字符串的切片引用(Slice)类型。其实当调用 + 的操作符时,相当于调用了 std::string 标准库中的 add() 方法,这里 add() 方法的第二个参数是一个引用的类型。因此我们在使用 +, 必须传递切片引用类型。不能直接传递 String 类型。+ 是返回一个新的字符串,所以变量声明可以不需要 mut 关键字修饰。

操作 UTF-8 字符串

rust
for c in "中国人".chars() {
  println!("{}", c);
}
for c in "中国人".chars() {
  println!("{}", c);
}
rust
for b in "中国人".bytes() {
    println!("{}", b);
}
for b in "中国人".bytes() {
    println!("{}", b);
}