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

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.