Add replace function for characters

This commit is contained in:
Stephen Chung
2020-05-12 23:30:55 +08:00
parent ec67879759
commit d6fd5416b0
2 changed files with 31 additions and 1 deletions

View File

@@ -226,6 +226,36 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
},
map,
);
reg_trinary_mut(
lib,
"replace",
|s: &mut String, find: String, sub: char| {
let new_str = s.replace(&find, &sub.to_string());
s.clear();
s.push_str(&new_str);
},
map,
);
reg_trinary_mut(
lib,
"replace",
|s: &mut String, find: char, sub: String| {
let new_str = s.replace(&find.to_string(), &sub);
s.clear();
s.push_str(&new_str);
},
map,
);
reg_trinary_mut(
lib,
"replace",
|s: &mut String, find: char, sub: char| {
let new_str = s.replace(&find.to_string(), &sub.to_string());
s.clear();
s.push_str(&new_str);
},
map,
);
reg_unary_mut(
lib,
"trim",