summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/strings/utf8replace_rs.rs
diff options
context:
space:
mode:
Diffstat (limited to 'misc/examples/strings/utf8replace_rs.rs')
-rw-r--r--misc/examples/strings/utf8replace_rs.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/misc/examples/strings/utf8replace_rs.rs b/misc/examples/strings/utf8replace_rs.rs
new file mode 100644
index 00000000..8b163b4e
--- /dev/null
+++ b/misc/examples/strings/utf8replace_rs.rs
@@ -0,0 +1,22 @@
+pub fn main() {
+ let mut hello = String::from("hell😀 w😀rld");
+ println!("{}", hello);
+
+ /* replace second smiley at utf8 codepoint pos 7 */
+ hello.replace_range(
+ hello
+ .char_indices()
+ .nth(7)
+ .map(|(pos, ch)| (pos..pos + ch.len_utf8()))
+ .unwrap(),
+ "🐨",
+ );
+ println!("{}", hello);
+
+ for c in hello.chars() {
+ print!("{},", c);
+ }
+
+ let str = "If you find the time, you will find the winner";
+ println!("\n{}", str.replace("find", "match"));
+}