Dart String

Summary: in this tutorial, you’ll learn how to use the Dart String type to manipulate strings effectively.

Introduction to the Dart String type

A string is a sequence of characters or more precisely a sequence of UTF-16 code units. Dart uses the String type to represent strings.

For example, the following declares a string variable:

String message;Code language: Dart (dart)

To create a string literal, you can use either single quotes or double quotes like this:

void main() {
  String s1 = 'A single-quoted string';
  String s2 = "A double-quoted string";
}Code language: Dart (dart)

If you want to use a single quote (') in a single-quoted string, you need to escape it using a backslash character. For example:

void main() {
  String message = 'It\'s me.';
  print(message);
}Code language: Dart (dart)

Output:

It's me.Code language: Dart (dart)

Similarly, if you use a double quote in a double-quoted string, you need to escape it using the backslash characters:

void main() {
  String message = "\"Dart is awesome!\". They said.";
  print(message);
}Code language: Dart (dart)

Output:

"Dart is awesome!". They said.Code language: Dart (dart)

Dart allows you to place a variable in a string using the $variableName syntax. For example:

void main() {
  String name = 'John';
  String message = 'Hello $name';
  print(message);
}Code language: Dart (dart)

Output:

Hello JohnCode language: Dart (dart)

In this example, Dart replaces the variable $name with its value in the message string. This is known as string interpolation.

To embed an expression in a string, you use the ${expression} syntax. For example:

void main() {
  var price = 10;
  var tax = 0.08;
  var message = 'The price with tax is ${price + price * tax}';

  print(message);
}Code language: Dart (dart)

Output:

The price with tax is 10.8Code language: Dart (dart)

Getting the length of a string

To get the length of a string, you use the length property:

str.lengthCode language: Dart (dart)

For example, the following program displays the length of the string 'hello':

void main() {
  var message = 'Hello';
  print(message.length);
}Code language: Dart (dart)

Output:

5Code language: Dart (dart)

Accessing individual characters in a string

To access an individual character in a string, you can use the square brackets [] with an index:

str[index]Code language: Dart (dart)

String indexing is zero-based. It means that the first character in the string has an index of 0, the next is 1, and so on. For example:

void main() {
  var message = 'Hello';

  print(message[0]);
  print(message[1]);
  print(message[2]);
  print(message[3]);
  print(message[4]);
}Code language: Dart (dart)

Output:

H
e
l
l
oCode language: Dart (dart)

Concatenating two strings

To concatenate two string literals, you use the + operator. For example:

void main() {
  var firstName = 'John';
  var lastName = 'Doe';
  var fullName = firstName + ' ' + lastName;

  print(fullName);
}Code language: Dart (dart)

Output:

John DoeCode language: Dart (dart)

In this example, we concatenate the first name, a space, and the last name into a string and assign it to the full name.

Also, you can use adjacent string literals for concatenation. For example:

void main() {
  var greeting = 'Hello' ' ' 'John';
  print(greeting);
}Code language: Dart (dart)

Output:

Hello JohnCode language: Dart (dart)

Dart strings are immutable

Dart strings are immutable. It means that you cannot modify a string. However, you can perform an operation on a string and assign the result to a new string. For example:

void main() {
  var message = 'Good Bye!';
  var greeting = message.substring(0, 4);

  print(greeting);
  print(message);
}Code language: Dart (dart)

Output:

Good
Good Bye!

In this example, we use the substring() method to get the first four characters of the message string and assign the result to the greeting string.

Dart multiline strings

To form a multiline string, you use triple quotes, either single or double quotes. For example:

void main() {
  var sql = '''select phone
  from phone_books
  where name =?''';

  print(sql);
}Code language: Dart (dart)

Output:

select phone
  from phone_books
  where name =?Code language: Dart (dart)

Summary

  • A string is a sequence of UTF-16 code units.
  • The String type represents strings.
  • Use the str.length property to get the length of the str.
  • Use str[index] to access a character at the index of the str.
  • Use + operator to concatenate two strings.
  • Strings are immutable.
  • Use triple quotes to form multiline strings.
Was this tutorial helpful ?