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.
- Appending character: This operation is helpful in appending character.
Example:// Scala program to append// a character// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating StringBuildervalx=newStringBuilder("Author");// Appending charactervaly=(x +='s')// Displays the string after// appending the characterprintln(y)}}chevron_rightfilter_noneOutput:Authors
Here, (x += ‘ ‘) is utilized to append a character.
- Appending String: This operation is helpful in appending string.
Example:// Scala program to append// a String// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating StringBuildervalx=newStringBuilder("Authors");// Appending Stringvaly=(x ++=" of GeeksforGeeks")// Displays the string after// appending the stringprintln(y)}}chevron_rightfilter_noneOutput:
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 program to append// String representation// of number// Creating objectobjectnum{// Main methoddefmain(args:Array[String]){// Creating StringBuildervalx=newStringBuilder("Number of Contributors : ");// Appending String// representation of numbervaly=x.append(800)// Displays the string after// appending the numberprintln(y)}}chevron_rightfilter_noneOutput: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 program to reset// the content// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating StringBuildervalx=newStringBuilder("Hello")// Resetting the contentvaly=x.clear()// Displays empty contentprintln(y)}}chevron_rightfilter_noneOutput:()
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 program to perform// delete operation// Creating objectobjectdelete{// Main methoddefmain(args:Array[String]){// Creating StringBuildervalq=newStringBuilder("Computer Science")// Deleting charactersvalr=q.delete(1,3)// Displaying string after// deleting some charactersprintln(r)}}chevron_rightfilter_noneOutput: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 program to perform// insertion operation// Creating objectobjectinsert{// Main methoddefmain(args:Array[String]){// Creating StringBuildervalq=newStringBuilder("GfG CS portal")// inserting stringsvalr=q.insert(4,"is a ")// Displays string after// insertion of required// stringprintln(r)}}chevron_rightfilter_noneOutput: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 program of Converting// StringBuilder to a String// Creating objectobjectbuilder{// Main methoddefmain(args:Array[String]){// Creating StringBuildervalq=newStringBuilder("GeeksforGeeks")// Applying conversion// operationvalr=q.toString// Displays Stringprintln(r)}}chevron_rightfilter_noneOutput:GeeksforGeeks
Here, q.toString is utilized to convert StringBuilder to a string.

