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:
object GFG
{
def main(args : Array[String])
{
val x = new StringBuilder( "Author" );
val y = (x + = 's' )
println(y)
}
}
|
Here, (x += ‘ ‘) is utilized to append a character.
- Appending String: This operation is helpful in appending string.
Example:
object GFG
{
def main(args : Array[String])
{
val x = new StringBuilder( "Authors" );
val y = (x ++ = " of GeeksforGeeks" )
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:
object num
{
def main(args : Array[String])
{
val x = new StringBuilder( "Number of Contributors : " );
val y = x.append( 800 )
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:
object GFG
{
def main(args : Array[String])
{
val x = new StringBuilder( "Hello" )
val y = x.clear()
println(y)
}
}
|
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:
object delete
{
def main(args : Array[String])
{
val q = new StringBuilder( "Computer Science" )
val r = q.delete( 1 , 3 )
println(r)
}
}
|
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:
object insert
{
def main(args : Array[String])
{
val q = new StringBuilder( "GfG CS portal" )
val r = q.insert( 4 , "is a " )
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:
object builder
{
def main(args : Array[String])
{
val q = new StringBuilder( "GeeksforGeeks" )
val r = q.toString
println(r)
}
}
|
Here, q.toString is utilized to convert StringBuilder to a string.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!