StringBuffer class in Dart:
The StringBuffer class of Dart is used to concatenate strings efficiently in Dart. In this post, we will learn how to use the StringBuffer class of dart with examples.
Constructor of StringBuffer:
The constructor of StringBuffer is:
StringBuffer([Object content = ""])
We have to pass the content to the StringBuffer constructor and it creates a StringBuffer with the given content.
For example:
void main() {
final buffer = StringBuffer('Hello World!!');
print(buffer);
}
It will create one StringBuffer object buffer. The parameter is optional. It will create the StringBuffer even if you don’t pass it to the constructor.
This program will print:
Hello World!!
How to add strings to the StringBuffer:
We can use the write function to add a string to the StringBuffer. It is defined as:
write(Object? object) → void
The below example shows how to use the write function:
void main() {
final buffer = StringBuffer();
buffer.write("Hello ");
buffer.write("World ");
buffer.write(123);
print(buffer);
}
It will print:
Hello World 123
How to add a newline:
We can use the writeln method to write one object with a new line at the end.
It is defined as:
writeln([Object? obj = ""]) → void
Let’s try it with an example:
void main() {
final buffer = StringBuffer();
buffer.writeln("Hello");
buffer.writeln();
buffer.writeln("World");
print(buffer);
}
It will add one new line at the end with the string. It will print:
Hello
World
How to add an iterable of objects:
There is one method in the StringBuffer class called writeAll that can be used to add all objects of an iterable.
writeAll(Iterable objects, [String separator = ""]) → void
Optionally, it also takes one separator as the second parameter. It will write all objects separated by the given separator.
void main() {
final buffer = StringBuffer();
buffer.writeAll([1, 2, 3, 4, 5], '-');
print(buffer);
}
It will print:
1-2-3-4-5
How to get the length of the StringBuffer:
The length property returns the length of a StringBuffer. This is an integer property.
void main() {
final buffer = StringBuffer();
buffer.write("Hello");
print(buffer.length);
}
It will print 5.
How to write character code to a StringBuffer:
The writeCharCode method of the StringBuffer class can write the string representation of a character code to the buffer.
writeCharCode(int charCode) → void
We can use this method as below:
void main() {
final buffer = StringBuffer();
buffer.writeCharCode(72);
buffer.writeCharCode(101);
buffer.writeCharCode(108);
buffer.writeCharCode(108);
buffer.writeCharCode(111);
print(buffer);
}
It will print Hello.
How to clear a StringBuffer:
We can use the clear() method to clear a buffer.
void main() {
final buffer = StringBuffer();
buffer.write("Hello");
print(buffer.length); //5
buffer.clear();
print(buffer.length); //0
}
The length of the buffer is 0 once it is cleared.
How to check if a StringBuffer is empty or not:
There are two properties to check if a StringBuffer is empty or not:
isEmpty -> bool
isNonEmpty -> bool
Both of these return boolean values.
You might also like:
- How to multiply strings in Dart
- What is string interpolation in dart
- try-catch in dart explanation with example
- Dart map() function explanation with example
- Dart remove items from a list that doesn’t satisfy a condition
- Get the only single element that satisfy a condition in dart list
- Dart program to get the first n elements from a list
- How to create a string from ASCII values in Dart