Home » Scala language

How to Replace 'bad' Characters in Scala?

There are some characters that are not displayed by all displays. In this tutorial, we will create a Scala method to replace a few bad characters.
Submitted by Shivang Yadav, on August 06, 2019

In Scala, programming language, all sorts of special characters are valid. The character set library is quite good and supports almost all characters in Scala programming.

But some displays do not support the use of all character and while programming this may create an issue as the display will misbehave and the output may get some errors. A display like Radio Pi and some web displays do not support characters like “, ” , ‘ , ’.

These characters are needed to be handled to get perfect output. For this in Scala, we can create a Scala method that replaces these bad characters with web-friendly once's.

Program to replace bad characters in Scala

object MyClass {
    def replacebad(s: String): String = {
        s.replaceAll("“", "\"")
        .replaceAll("”", "\"")
        .replaceAll("‘", "\"")
        .replaceAll("’", "\"")
    }
    
    def main(args: Array[String]) {
        println(replacebad("“"))
        println(replacebad("”"))
        println(replacebad("‘"))
        println(replacebad("’"))
    }
}

Output

"
"
"
"



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.