C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Append: This method receives many types of arguments. But most commonly we add strings to a StringBuilder.
Length: This is the number of characters within the StringBuilder. We can use "size" as an alternative.
ToString: Often we need a string to pass to another function as an argument. With toString we convert the StringBuilder to a string.
Scala program that creates StringBuilder, append strings
// Create a new StringBuilder.
// ... Append two strings.
val builder = StringBuilder.newBuilder
builder.append("cat ")
builder.append("bird")
// Print the StringBuilder and its length.
println(builder)
println(builder.length)
// Convert StringBuilder to a string.
val result = builder.toString()
println(result)
Output
cat bird
8
cat bird
For: We loop over the "format" string with a for-loop. When the special chars are encountered, we append Ints to the StringBuilder.
ForResult: The StringBuilder has numbers in place of the format chars. We use toString to get a final string representation.
Scala program that appends characters with if, else
// Use this as a custom format string.
val format = "Cats: @, dogs: %"
println(format)
// Create a new StringBuilder.
val builder = StringBuilder.newBuilder
val cats = 10
val dogs = 20
// Loop over the format string.
// ... Append values in place of format characters.
for (i <- format.indices) {
if (format(i) == '@') {
builder.append(cats)
}
else if (format(i) == '%') {
builder.append(dogs)
}
else {
builder.append(format(i))
}
}
// Print the results.
val result = builder.toString()
println(result)
Output
Cats: @, dogs: %
Cats: 10, dogs: 20
Insert: With insert, we place a string beginning at an index. Later characters are moved to be after the new string.
Replace: This receives a start index, and an end index (not a length). We replace the first char with the arguments 0, 1.
Scala program that uses insert, replace
val builder = StringBuilder.newBuilder
// Append initial data.
builder.append("I like cats")
println(builder)
// Insert this string at index 1.
builder.insert(1, " do not")
println(builder)
// Replace first character with a new string.
builder.replace(0, 1, "You")
println(builder)
Output
I like cats
I do not like cats
You do not like cats
Important: This function returns a string. Unlike replace() it does not modify the StringBuilder's buffer.
Scala program that uses replaceAllLiterally
val builder = StringBuilder.newBuilder
builder.append("two dogs, four dogs")
// Replace dogs with cats.
// ... A new string is returned.
val result1 = builder.replaceAllLiterally("dogs", "cats")
println(result1)
// Replace two with three.
val result2 = builder.replaceAllLiterally("two", "three")
println(result2)
Output
two cats, four cats
three dogs, four dogs
Scala program that gets characters, uses charAt
val letters = StringBuilder.newBuilder
letters.append("abc")
// Get the first char.
// ... We can access the builder like a list or array.
val firstChar = letters(0)
// Use charAt to get second and last chars.
val secondChar = letters.charAt(1)
val lastChar = letters.charAt(letters.length - 1)
// Print our characters.
println(firstChar)
println(secondChar)
println(lastChar)
Output
a
b
c
Scala program that causes out of range error
val names = StringBuilder.newBuilder
names.append("mars")
// This will cause an error.
val c = names(400)
Output
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 400
at java.lang.AbstractStringBuilder. charAt(Unknown Source)
at java.lang.StringBuilder. charAt(Unknown Source)
at scala.collection.mutable. StringBuilder.apply(StringBuilder.scala:117)...