String chomp(or chop) function in Scala

Scala | String chomp (or chop) function: Here, we will learn how to chop off the end of line statements in Scala programming?
Submitted by Shivang Yadav, on May 16, 2020

Chop or Chomp string

It is used to chop off the end of line characters. For this Scala has an inbuilt method stripLineEnd.

Syntax:

    string.stripLineEnd

Program to chop/Strip String in Scala

object MyClass {
    def main(args: Array[String]) {
        val str = "rfgdg\n"
        println("The string is '" + str.stripLineEnd + "' ") 
    }
}

Output:

The string is 'rfgdg' 

A string chomp method can be created like this,

    def chomp(str: String) = str.stripLineEnd

Example:

object MyClass {
    // function definition
    def chomp(str: String) = str.stripLineEnd
    
    def main(args: Array[String]) {
        val text = "Hello world!\n"
    
        // function calling
        println("chomp(text): " + chomp(text))
    }
}

Output:

chomp(text): Hello world!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




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.