How do I replace specific characters idiomatically in Rust?

Vin

So I have the string "Hello World!" and want to replace the "!" with "?" so that the new string is "Hello World?"

In Ruby we can do this easily with the gsub method:

"Hello World!".gsub("!", "?")

How to do this idiomatically in Rust?

fny

You can replace all occurrences of one string within another with std::str::replace:

let result = str::replace("Hello World!", "!", "?");
// Equivalently
result = "Hello World!".replace("!", "?");
println!("{}", result); // => "Hello World?"

For more complex cases, you can use regex::Regex::replace_all from regex:

use regex::Regex;
let re = Regex::new(r"[A-Za-z]").unwrap();
let result = re.replace_all("Hello World!", "x");
println!("{}", result); // => "xxxxx xxxxx!"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I replace characters before a specific character?

From Dev

How do I parse a string consisting of a letter and number idiomatically and efficiently in Rust?

From Dev

How do I idiomatically convert a BOOL to a bool?

From Dev

How do you replace specific characters in beautifulSoup?

From Dev

How do I replace illegal characters in a filename?

From Dev

How do I replace characters with a variable in CMD?

From Dev

How do I convert between numeric types safely and idiomatically?

From Dev

How do I capitalize all the characters in a string in Rust?

From Dev

How to replace 2 specific characters with a specific charecter

From Dev

How do I replace characters in every string in my list in java?

From Dev

How do I cycle through a list of characters and replace them with "_" in Python?

From Dev

How do i search & replace using sed and not include a group of characters?

From Dev

How do I replace space characters in a string with "%20"?

From Dev

How do I replace the first three characters of a Java integer by 111?

From Dev

How do I replace a multiple sets of characters with a single character

From Dev

How do I replace non word characters with a dash using TSQL?

From Dev

How do I replace characters only on certain lines?

From Dev

How do I replace characters in a string using a character map?

From Dev

How do I replace a certain combination of characters / inputs?

From Dev

How do I parse/skip specific characters in attrparsec?

From Dev

How do I pass a specific number of characters from an istream to an ostream

From Dev

How do I set a range for Word in vba for a specific set of characters?

From Dev

How do I display all the characters between two specific strings?

From Dev

How do I find specific characters in a given string?

From Dev

How do I set a range for Word in vba for a specific set of characters?

From Dev

How do I search with Excel for cells with specific characters at the end of the string?

From Dev

How do I only show values that end in a specific characters?

From Dev

How do I replace only specific match in a match collection?

From Dev

How do I replace a specific string word in data- attribute?

Related Related

  1. 1

    How do I replace characters before a specific character?

  2. 2

    How do I parse a string consisting of a letter and number idiomatically and efficiently in Rust?

  3. 3

    How do I idiomatically convert a BOOL to a bool?

  4. 4

    How do you replace specific characters in beautifulSoup?

  5. 5

    How do I replace illegal characters in a filename?

  6. 6

    How do I replace characters with a variable in CMD?

  7. 7

    How do I convert between numeric types safely and idiomatically?

  8. 8

    How do I capitalize all the characters in a string in Rust?

  9. 9

    How to replace 2 specific characters with a specific charecter

  10. 10

    How do I replace characters in every string in my list in java?

  11. 11

    How do I cycle through a list of characters and replace them with "_" in Python?

  12. 12

    How do i search & replace using sed and not include a group of characters?

  13. 13

    How do I replace space characters in a string with "%20"?

  14. 14

    How do I replace the first three characters of a Java integer by 111?

  15. 15

    How do I replace a multiple sets of characters with a single character

  16. 16

    How do I replace non word characters with a dash using TSQL?

  17. 17

    How do I replace characters only on certain lines?

  18. 18

    How do I replace characters in a string using a character map?

  19. 19

    How do I replace a certain combination of characters / inputs?

  20. 20

    How do I parse/skip specific characters in attrparsec?

  21. 21

    How do I pass a specific number of characters from an istream to an ostream

  22. 22

    How do I set a range for Word in vba for a specific set of characters?

  23. 23

    How do I display all the characters between two specific strings?

  24. 24

    How do I find specific characters in a given string?

  25. 25

    How do I set a range for Word in vba for a specific set of characters?

  26. 26

    How do I search with Excel for cells with specific characters at the end of the string?

  27. 27

    How do I only show values that end in a specific characters?

  28. 28

    How do I replace only specific match in a match collection?

  29. 29

    How do I replace a specific string word in data- attribute?

HotTag

Archive