×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

String chomp(or chop) function in Scala

By IncludeHelp Last updated : October 20, 2024

Description and Usage

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

Syntax

Use the below syntax for string chopping off:

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' 

Creating string chomp() function

A string chomp() function can be created like this,

Syntax

Use the below syntax to create string chomp() function:

def chomp(str: String) = str.stripLineEnd

Example: Creating string chomp() function

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.