How to replace characters from a given string.
I wrote the following code, but here it iterates over the chars.
val chars = "\\`*{}[]()<>#+:~'%^&@?;,.\"!$=|"
var newString = inputString
chars.map { c =>
if (inputString.contains(c)){
newString = newString.replace(c, '_')
}
}
I'm looking at code similar to below. Unfortunately, it is throwing an error. Can someone help me to figure out the error?
scala> "hello:world".replaceAll("\\`*{}[]()>#+:~'%^&@<?;,\"!$=|.", "_")
java.util.regex.PatternSyntaxException: Illegal repetition near index 2
\`*{}[]()>#+:~'%^&@<?;,"!$=|.
^
It turns out that the first argument needs to be a regular expression for the argument, and not a list of characters, but I still get an error:
scala> "hello:world".replaceAll("[`*{}[]()>#+:~'%^&@<?;,\"!$=|.]", "_")
java.util.regex.PatternSyntaxException: Unclosed character class near index 29
[`*{}[]()>#+:~'%^&@<?;,"!$=|.]
^