×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Misc. Topics

Scala Practice

How to Replace 'bad' Characters in Scala?

By IncludeHelp Last updated : October 20, 2024

What are bad Characters in Scala?

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.

How to Replace 'bad' Characters?

For this in Scala, we can create a Scala method that replaces these bad characters with web-friendly once's.

Scala Code to Replace 'bad' Characters

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.