Dart StringBuffer length
Syntax & Examples


StringBuffer.length property

The `length` property in Dart returns the length of the content that has been accumulated so far. It is a constant-time operation that provides the number of elements in a collection, string, or list.


Syntax of StringBuffer.length

The syntax of StringBuffer.length property is:

 int length 

This length property of StringBuffer returns the length of the content that has been accumulated so far. This is a constant-time operation.

Return Type

StringBuffer.length returns value of type int.



✐ Examples

1 Get the length of a string

In this example,

We create a string `str` with the value 'Hello, world!'. We use the `length` property to get the length of the string, which is the number of characters in the string.

Dart Program

void main() {
  String str = 'Hello, world!';
  int strLength = str.length;
  print('Length of str: $strLength');
}

Output

Length of str: 13

2 Get the length of a list of numbers

In this example,

We create a list `numbers` with some integer elements. We then use the `length` property to get the length of the list, which is the number of elements in the list.

Dart Program

void main() {
  List<int> numbers = [1, 2, 3, 4, 5];
  int numbersLength = numbers.length;
  print('Length of numbers list: $numbersLength');
}

Output

Length of numbers list: 5

3 Get the length of a set of names

In this example,

We create a set `names` with some string elements. We use the `length` property to get the length of the set, which is the number of elements in the set.

Dart Program

void main() {
  Set<String> names = {'Alice', 'Bob', 'Charlie'};
  int namesLength = names.length;
  print('Length of names set: $namesLength');
}

Output

Length of names set: 3

Summary

In this Dart tutorial, we learned about length property of StringBuffer: the syntax and few working examples with output and detailed explanation for each example.