I am trying to create a method to convert characters within a String, specifically converting all '0' to ' '. This is the code that I am using:
def removeZeros(s: String) = {
val charArray = s.toCharArray
charArray.map( c => if(c == '0') ' ')
new String(charArray)
}
Is there a simpler way to do it? This syntax is not valid:
def removeZeros(s: String) =
new String(s.toCharArray.map( c => if(c == '0') ' '))
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', ' ')