StringBuilder in Scala Last Updated : 29 Mar, 2019 Comments Improve Suggest changes Like Article Like Report A String object is immutable, i.e. a String cannot be changed once created. In situations where you need to perform repeated modifications to a string, we need StringBuilder class. StringBuilder is utilized to append input data to the internal buffer. We can perform numerous operations with the support of methods on the StringBuilder. This operation comprises appending data, inserting data, and removing data. important points: The StringBuilder class is beneficent for mutable strings to extend effectively. The instance of StringBuilder is utilized like a String. Strings of Scala are immutable so, when you require a mutable String then you can use StringBuilder. Operations performed by the StringBuilder class Appending character: This operation is helpful in appending character. Example: Scala // Scala program to append // a character // Creating object object GFG { // Main method def main(args: Array[String]) { // Creating StringBuilder val x = new StringBuilder("Author"); // Appending character val y = (x += 's') // Displays the string after // appending the character println(y) } } Output: Authors Here, (x += ' ') is utilized to append a character. Appending String: This operation is helpful in appending string. Example: Scala // Scala program to append // a String // Creating object object GFG { // Main method def main(args: Array[String]) { // Creating StringBuilder val x = new StringBuilder("Authors"); // Appending String val y = (x ++= " of GeeksforGeeks") // Displays the string after // appending the string println(y) } } Output: Authors of GeeksforGeeks Here, (x ++= ' ') is utilized to append String. Appending String representation of number: Here, the number can be of any type like Integer, Double, Long, Float, etc. Example: Scala // Scala program to append // String representation // of number // Creating object object num { // Main method def main(args: Array[String]) { // Creating StringBuilder val x = new StringBuilder("Number of Contributors : "); // Appending String // representation of number val y = x.append(800) // Displays the string after // appending the number println(y) } } Output: Number of Contributors : 800 Here, x.append(n) is utilized to append the String representation of the number, where 'n' is the number of any type. Resetting the content of the StringBuilder: It is helpful in resetting the content by making it empty. Example: Scala // Scala program to reset // the content // Creating object object GFG { // Main method def main(args: Array[String]) { // Creating StringBuilder val x = new StringBuilder("Hello") // Resetting the content val y = x.clear() // Displays empty content println(y) } } Output: () Here, x.clear() is utilized to clear the content of the StringBuilder. Delete operation: This operation is helpful in deleting characters from the content of the StringBuilder. Example: Scala // Scala program to perform // delete operation // Creating object object delete { // Main method def main(args: Array[String]) { // Creating StringBuilder val q = new StringBuilder("Computer Science") // Deleting characters val r = q.delete(1, 3) // Displaying string after // deleting some characters println(r) } } Output: Cputer Science Here, q.delete(i, j) is utilized to delete the character indexed from i to (j - 1). Insertion operation: This operation is helpful in inserting Strings. Example: Scala // Scala program to perform // insertion operation // Creating object object insert { // Main method def main(args: Array[String]) { // Creating StringBuilder val q = new StringBuilder("GfG CS portal") // inserting strings val r = q.insert(4, "is a " ) // Displays string after // insertion of required // string println(r) } } Output: GfG is a CS portal Here, q.insert(i, "s") is utilized to insert the String (s) at index i. Converting StringBuilder to a String: StringBuilder can be converted to a String using this operation. Example: Scala // Scala program of Converting // StringBuilder to a String // Creating object object builder { // Main method def main(args: Array[String]) { // Creating StringBuilder val q = new StringBuilder("GeeksforGeeks") // Applying conversion // operation val r = q.toString // Displays String println(r) } } Output: GeeksforGeeks Here, q.toString is utilized to convert StringBuilder to a string. Comment More info N nidhi1352singh Follow Improve Article Tags : Scala Scala Scala-Strings Explore OverviewScala Programming Language3 min readIntroduction to Scala7 min readSetting up the environment in Scala3 min readHello World in Scala2 min readBasicsScala Keywords2 min readScala Identifiers3 min readData Types in Scala3 min readVariables in Scala3 min readControl StatementsScala | Decision Making (if, if-else, Nested if-else, if-else if)5 min readScala | Loops(while, do..while, for, nested loops)5 min readBreak statement in Scala3 min readScala | Literals4 min readOOP ConceptsClass and Object in Scala5 min readInheritance in Scala5 min readOperators in Scala11 min readScala Singleton and Companion Objects3 min readScala Constructors4 min readScala | Polymorphism5 min readScala | Multithreading3 min readScala this keyword2 min readMethodsScala | Functions - Basics3 min readAnonymous Functions in Scala2 min readScala | Closures3 min readRecursion in Scala4 min readMethod Overloading in Scala5 min readMethod Overriding in Scala8 min readLambda Expression in Scala4 min readScala Varargs2 min readStringsScala String4 min readScala | String Interpolation3 min readScala | StringContext2 min readRegular Expressions in Scala5 min readStringBuilder in Scala4 min readScala PackagesPackages In Scala4 min readScala | Package Objects3 min readChained Package Clauses in Scala3 min readFile Handling in Scala3 min readScala TraitScala | Traits7 min readScala | Sealed Trait4 min readScala | Trait Mixins3 min readTrait Linearization in Scala5 min readCollectionsScala Lists5 min readScala ListBuffer6 min readListSet in Scala6 min readScala Map5 min readScala | Arrays6 min readScala | ArrayBuffer4 min readScala | Tuple5 min readSet in Scala | Set-13 min readSet in Scala | Set-27 min readBitSet in Scala5 min readHashSet In Scala4 min readStack in Scala3 min readHashMap in Scala3 min readTreeSet in Scala4 min readIterators in Scala5 min readScala | Option3 min read Like