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!



Comments and Discussions!

Load comments ↻






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