Replacing characters in a String in Scala
You can map strings directly:
def removeZero(s: String) = s.map(c => if(c == '0') ' ' else c)
alternatively you could use replace
:
s.replace('0', ' ')
Very simple:
scala> "FooN00b".filterNot(_ == '0')
res0: String = FooNb
To replace some characters with others:
scala> "FooN00b" map { case '0' => 'o' case 'N' => 'D' case c => c }
res1: String = FooDoob
To replace one character with some arbitrary number of characters:
scala> "FooN00b" flatMap { case '0' => "oOo" case 'N' => "" case c => s"$c" }
res2: String = FoooOooOob