Dart String Data Type by Examples

In this Dart tutorial, we learn how to use String in Dart programming language via different example Dart programs.

How to declare String variables in Dart

Declare single line String with single quotes or double quotes.

void main() {
  String str1 = "Hello MobileCode.Dev";
  String str2 = 'Hello MobileCode.Dev';
  String str3 = "Hello 'MobileCode.Dev'";
  String str4 = 'Hello "MobileCode.Dev"';

  print(str1);
  print(str2);
  print(str3);
  print(str4);
}
The output is:
Hello MobileCode.Dev
Hello MobileCode.Dev
Hello 'MobileCode.Dev'
Hello "MobileCode.Dev"

Declare multi lines String triple quotes.

void main() {
  String str1 = """ This is
  a 
  multi lines 
  String in Dart
  """;

  String str2 = '''
  Hello
  MobileCode.Dev
  ''';

  print(str1);
  print(str2);
}
The output is:
 This is
  a
  multi lines
  String in Dart

  Hello
  MobileCode.Dev

How to concatenate Strings in Dart

Use plus ( + ) operator to concatenate Strings.

void main() {
  String str = "Hello, " + "MobileCode.Dev";
  
  print(str);
}
The output is:
Hello, MobileCode.Dev

Use adjacent string literals to concatenate Strings in Dart.

void main() {
  String str = "Hello, " " MobileCode.DEV";

  print(str);
}
The output is:
Hello,  MobileCode.DEV

Use * operator to concatenate String multiple times.

void main() {
  String str = "test ";
  
  print(str * 5);
}
The output is:
test test test test test

String interpolation in Dart

In Dart programming language, we can use ${} to interpolate the value of Dart expressions within Strings.

void main() {
  double price = 10.5;
  String productName = "Product Name";
  String message = "The ${productName} price is ${price}";

  print(message);
}
The output is:
The Product Name price is 10.5

Check the String value is empty or not

Using isEmpty property

void main() {
  String str = "";
  if(str.isEmpty) {
    print('String is empty');
  } else {
    print('String is not empty');
  }
}
The output is:
String is empty

void main() {
  String str = "mobilecode.dev";
  if(str.isEmpty) {
    print('String is empty');
  } else {
    print('String is not empty');
  }
}
The output is:
String is not empty

Using isNotEmpty property

void main() {
  String str = "";
  if(str.isNotEmpty) {
    print('String is not empty');
  } else {
    print('String is empty');
  }
}
The output is:
String is empty

void main() {
  String str = "mobilecode.dev";
  if(str.isNotEmpty) {
    print('String is not empty');
  } else {
    print('String is empty');
  }
}
The output is:
String is not empty

Get length of a String

String class provides length property to get the length of a String.

void main() {
  String str = "mobilecode.dev";

  print(str.length);
}
The output is:
14

Check if a String end with a given String using endsWith() method

void main() {
  String str = "mobilecode.dev";

  print(str.endsWith("dev"));
  print(str.endsWith("code"));
}
The output is:
true
false

Get a subString with given indexes using substring() method

void main() {
  String str = 'mobilecode.dev';

  print(str.substring(0, 10));
}
The output is:
mobilecode

Convert all characters in a String to lower cases using toLowerCase() method

void main() {
  String str = "MOBILECode.Dev";

  print(str.toLowerCase());
}
The output is:
mobilecode.dev

Convert all characters in a String to upper cases using toUpperCase() method

void main() {
  String str = "mobilecode.dev";

  print(str.toUpperCase());
}
The output is:
MOBILECODE.DEV

Get the list of the UTF-16 code units of a String

void main() {
  String str = "mobilecode.dev";
  
  print(str.codeUnits);
}
The output is:
[109, 111, 98, 105, 108, 101, 99, 111, 100, 101, 46, 100, 101, 118]

Get Unicode code-points of a String using runes property

void main() {
  String str1 = "xin chào";
  String str2 = "こんにちは";

  print(str1.runes);
  print(str2.runes);
}
The output is:
(120, 105, 110, 32, 99, 104, 224, 111)
(12371, 12435, 12395, 12385, 12399)

Get code unit at a given index using codeUnitAt() method

void main() {
  String str = "mobilecode.dev";

  print(str.codeUnitAt(5));
}
The output is:
101

Compare two String using compareTo() method

void main() {
  String str1 = "a";
  
  print(str1.compareTo("A"));
  print(str1.compareTo("z"));
  print(str1.compareTo("a"));
}
The output is:
1
-1
0

How to use trim() method

The trim() method to get a new String without any leading and trailing whitespace.

void main() {
  String str = "  Dart    Tutorial  ";

  print(str.trim());
}
The output is:
Dart    Tutorial

How to use trimLeft() method

The trimLeft() method to get a new String without any leading whitespace.

void main() {
  String str = "  Dart    Tutorial  ";

  print(str.trimLeft());
}
The output is:
Dart    Tutorial

How to use trimRight() method

The trimRight() method to get a new String without any trailing whitespace.

void main() {
  String str = "  Dart    Tutorial  ";

  print(str.trimRight());
}
The output is:
  Dart    Tutorial

Happy Coding 😊

Dart StringBuffer by Examples

Dart integer Data Type by Examples

Dart double Data Type by Examples

Dart bool Data Type by Examples

Dart RegExp by Examples

Tags: