Joe Joe - 1 year ago 68
Scala Question

how to replace multiple chars efficiently with scala?

I want to replace 5 different chars to 5 different chars, and the current way im doing it looks stupid, so what would be a better way to do this?


private def fixChars(str: String): String = {
str.replaceAll("ø", "o")


Answer Source

I believe this approach will work for you and takes only one iteration to substitute all characters:

private def fixChars(str: String): String = {
  val substitutions = Map(
    'Ø' -> 'O',
    'ø' -> 'o',
  ) => substitutions.getOrElse(c, c))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download