StringBuilder in Scala

Here, we will learn about StringBuilder in Scala. A StringBuilder is used to create an internal buffer. We will learn about operations, and example on StringBuilder.
Submitted by Shivang Yadav, on December 14, 2020

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.

Here are some operations that can be performed using StringBuilder

1) Creating a StringBuffer

StringBuffer 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 StringBuffer

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

Output:

Scala Programming Language

2) Appending new Character to StringBuffer

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

Syntax:

val newStr = strBuf += 'char'

Program to illustrate appending of character to StringBuffer

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

Output:

Scala Programming Languages

3) Appending a string to StringBuffer

You can append a new string to the StringBuffer i.e. add a string at the end of the StringBuffer. 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 StringBuffer

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

Output:

Scala Programming language

4) Insertion at a given position

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

Syntax:

insert(index, "string")

Parameter: 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 StringBuffer.

Program to illustrate the insertion of string at a given position

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

Output:

Scala is Programming 

5) Resetting StringBuffer

Resetting StringBuffer 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 StringBuffer in Scala

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

Output:

()

6) Deleting StringBuffer

You can delete characters from the StringBuffer 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 StringBuffer

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

Output:

Scala Language

7) Conversion of StringBuffer to String

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

Syntax:

toString

Program to illustrate the conversion of StringBuffer to String

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

Output:

Scala Programming Language




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.