×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

StringBuilder in Scala

By IncludeHelp Last updated : October 20, 2024

What is StringBuilder in Scala?

StringBuilder is a class that is used to append the data inputted to the internal Buffer. It is used on mutable strings to perform operations. You can perform multiple operations of the StringBuilder object to updates characters to it.

It is the instance of the StringBuilder class that makes the update operation on mutable string really easy.

Creating a StringBuilder

StringBuilder is a class used to create a new one, we need the new keyword and create objects.

Syntax

val obj_Name = new StringBuilder("string")

Program to illustrate creating a StringBuilder

object myObject {
    def main(args: Array[String]) {
        // Creating a StringBuilder...
        val strBuf = new StringBuilder("Scala Programming Language")
        println(strBuf)
    }
}

Output:

Scala Programming Language

Appending new character to StringBuilder

You can also append a new character to the StringBuilder i.e. add the character at the end of the StringBuilder. It is done by using the += method.

Syntax

val newStr = strBuf += 'char'

Program to illustrate appending of character to StringBuilder

object myObject {
    def main(args: Array[String]) {
        // Creating a StringBuilder...
        val strBuf = new StringBuilder("Scala Programming Language")
        
        //Appending charater to StringBuilder
        val newStr = strBuf + "s"
        println(newStr)
    }
}

Output:

Scala Programming Languages

Appending a string to StringBuilder

You can append a new string to the StringBuilder i.e. add a string at the end of the StringBuilder. It is done by using the ++= keyword followed by the string.

Syntax

val newStr = strBuf ++= "String"

Program to illustrate the appending of string to a StringBuilder

object myObject {
    def main(args: Array[String]) {
        // Creating a StringBuilder...
        val strBuf = new StringBuilder("Scala Programming ")
        
        //Appending string to StringBuilder
        val newStr = strBuf + "language"
        println(newStr)
    }
}

Output:

Scala Programming language

Insertion at a given position

We can even insert a string at a given position in the StringBuilder. We can use the insert function to add the string at the given position.

Syntax

insert(index, "string")

Parameters

The method takes two parameters, index which is the index where the string needs to be inserted. And the other is string which is to be inserted in the StringBuilder.

Program to illustrate the insertion of string at a given position

object myObject {
    def main(args: Array[String]) {
        // Creating a StringBuilder...
        val strBuf = new StringBuilder("Scala Programming ")
        
        //inseting a to StringBuilder
        val newStr = strBuf.insert(6, "is ")
        println(newStr)
    }
}

Output:

Scala is Programming 

Resetting StringBuilder

Resetting StringBuilder is making it empty, i.e. deleting all the existing string from the Buffer. It is done by using a clear method.

Syntax

clear()

Program to reset StringBuilder in Scala

object myObject {
    def main(args: Array[String]) {
        // Creating a StringBuilder...
        val strBuf = new StringBuilder("Scala Programming Language") 
        
        val newStr = strBuf.clear()
        println(newStr)
    }
}

Output:

()

Deleting StringBuilder

You can delete characters from the StringBuilder within some positions. Their characters will be deleted from a starting index to an ending index.

Syntax

delete(i, j)

Parameters

There are two parameters to the method, starting index and ending index.

Program to illustrate deleting StringBuilder

object myObject {
    def main(args: Array[String]) {
        // Creating a StringBuilder...
        val strBuf = new StringBuilder("Scala Programming Language") 
        
        val newStr = strBuf.delete(6, 18)
        println(newStr)
    }
}

Output:

Scala Language

Conversion of StringBuilder to String

If you want to convert the StringBuilder to a string, you can to it using toString method.

Syntax

toString

Program to illustrate the conversion of StringBuilder to String

object myObject {
    def main(args: Array[String]) {
        // Creating a StringBuilder...
        val strBuf = new StringBuilder("Scala Programming Language") 
        
        val newStr = strBuf.toString
        println(newStr)
    }
}

Output:

Scala Programming Language

Comments and Discussions!

Load comments ↻





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