I am not very versed in programming in general so please cut me some slack here. Is there a more elegant way to tackle this process of replacing multiple characters in a string?
strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(godiacritics.Normalize(strings.ToLower(articles[i].Name)), "-", "_"), " ", "_"), ",", "_"), ".", ""), "/", ""), "€", ""), "%", ""), "12", "halb"), "14", "viertel")
If you want to replace multiple characters you can call the String. prototype. replace() with the replacement argument being a function that gets called for each match. All you need is an object representing the character mapping which you will use in that function.
SELECT REPLACE(REPLACE(REPLACE(REPLACE('3*[4+5]/{6-8}', '[', '('), ']', ')'), '{', '('), '}', ')'); We can see that the REPLACE function is nested and it is called multiple times to replace the corresponding string as per the defined positional values within the SQL REPLACE function.
A character in Python is also a string. So, we can use the replace() method to replace multiple characters in a string. It replaced all the occurrences of, Character 's' with 'X'.
Use the replace() method to replace multiple characters in a string, e.g. str. replace(/[. _-]/g, ' ') . The first parameter the method takes is a regular expression that can match multiple characters.
Alternatively, you can use the String.replaceAll method. To replace multiple characters in a string, chain multiple calls to the replaceAll () method, e.g. str.replaceAll ('.', '!').replaceAll ('_', '!'). The replaceAll method will return a new string, where all occurrences of the characters are replaced by the provided replacement.
Use str.replace () to Replace Multiple Characters in Python We can use the replace () method of the str data type to replace substrings into a different output. replace () accepts two parameters, the first parameter is the regex pattern you want to match strings with, and the second parameter is the replacement string for the matched strings.
The replaceAll method will return a new string, where all occurrences of the characters are replaced by the provided replacement. We passed the following 2 arguments to the replaceAll method: This approach is a bit more verbose then using a regular expression with a character class.
Use the SUBSTITUTE Function to Substitute Multiple Characters In Excel, the SUBSTITUTE Function substitutes one or more instances of a specified character or text string with another character (s). In the below screenshot, here is a data set of Microsoft Word versions names. For an instance, we want to substitute “ Word ” with “ Excel ”.
Create a single strings.Replacer
which contains all replaceable pairs:
r := strings.NewReplacer(
"-", "_",
" ", "_",
",", "_",
".", "",
"/", "",
"€", "",
"%", "",
"12", "halb",
"14", "viertel",
)
And use it like this:
s2 := r.Replace(godiacritics.Normalize(strings.ToLower(articles[i].Name)))
strings.Replacer
performs all replaces in a single step (it iterates over the string once). It's also safe for concurrent use, create the Replacer
onces and reuse it whenever / wherever needed.
Example code to test it:
s := "test- ,./€%:12 14"
s2 := strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, "-", "_"), " ", "_"), ",", "_"), ".", ""), "/", ""), "€", ""), "%", ""), "12", "halb"), "14", "viertel")
fmt.Println(s2)
r := strings.NewReplacer(
"-", "_",
" ", "_",
",", "_",
".", "",
"/", "",
"€", "",
"%", "",
"12", "halb",
"14", "viertel",
)
s3 := r.Replace(s)
fmt.Println(s3)
Which outputs (try it on the Go Playground):
test___:halb_viertel
test___:halb_viertel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With