代码点读机 / Rust / replace

replace Rust · 常用方法

把字符串中所有匹配的子串替换成另一个子串,返回新的 String。

怎么用

s.replace(旧子串, 新子串);,用于替换子串

小例子

let s = "hello".replace("l", "x");
println!("{}", s);

运行结果

hexxo

提醒:替换所有匹配处;要只替换一次用 replacen

官方文档:https://doc.rust-lang.org/std/primitive.str.html#method.replace

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Rust全部词条