Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace multiple characters in the same string?

Tags:

string

go

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")
like image 985
Michael Pietzsch Avatar asked Oct 04 '21 18:10

Michael Pietzsch


People also ask

How do I replace multiple characters in a string?

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.

How do I replace multiple characters in a string in SQL?

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.

How do you replace multiple characters in a list Python?

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'.

How do you replace multiple letters in a string in Java?

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.

How do I replace multiple characters in a string in Java?

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.

How do you replace a string with a different string?

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.

How to replace all occurrences of a character in a string?

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.

How to substitute multiple characters in Excel?

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 ”.


Video Answer


1 Answers

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
like image 82
icza Avatar answered Oct 17 '22 15:10

icza