Strings in Dart

The Dart programming language has many handy string manipulation features, which are commonly used and essential to most applications.

void main() {
  String fruit = 'orange';
  print(fruit.length);
}
// Result: 6

Sometimes you may want to use the string length value directly within a string value, which can be done using curly brackets after the dollar sign.

void main() {
  String fruit = 'orange';
  print("The word orange has ${fruit.length} letters.");
}
// Result: The word orange has 6 letters.

If you wanted double quotation marks around the word “orange” to make it stand out, then one way to do this is using the backslash character \ to escape or tell Dart to treat that character as part of the string value, like below.

void main() {
  String fruit = 'orange';
  print("The word \"orange\" has ${fruit.length} letters.");
}
// Result: The word "orange" has 6 letters.

Although, another way to do this is to use single quotes to indicate the beginning and end of the string text value.

void main() {
  String fruit = 'orange';
  print('The word "orange" has ${fruit.length} letters.');
}
// Result: The word "orange" has 6 letters.

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published.

14 − fourteen =