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);
}
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);
}
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);
}
Hello, MobileCode.Dev
Use adjacent string literals to concatenate Strings in Dart.
void main() {
String str = "Hello, " " MobileCode.DEV";
print(str);
}
Hello, MobileCode.DEV
Use * operator to concatenate String multiple times.
void main() {
String str = "test ";
print(str * 5);
}
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 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');
}
}
String is empty
void main() {
String str = "mobilecode.dev";
if(str.isEmpty) {
print('String is empty');
} else {
print('String is not empty');
}
}
String is not empty
Using isNotEmpty property
void main() {
String str = "";
if(str.isNotEmpty) {
print('String is not empty');
} else {
print('String is empty');
}
}
String is empty
void main() {
String str = "mobilecode.dev";
if(str.isNotEmpty) {
print('String is not empty');
} else {
print('String is empty');
}
}
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);
}
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"));
}
true
false
Get a subString with given indexes using substring() method
void main() {
String str = 'mobilecode.dev';
print(str.substring(0, 10));
}
mobilecode
Convert all characters in a String to lower cases using toLowerCase() method
void main() {
String str = "MOBILECode.Dev";
print(str.toLowerCase());
}
mobilecode.dev
Convert all characters in a String to upper cases using toUpperCase() method
void main() {
String str = "mobilecode.dev";
print(str.toUpperCase());
}
MOBILECODE.DEV
Get the list of the UTF-16 code units of a String
void main() {
String str = "mobilecode.dev";
print(str.codeUnits);
}
[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);
}
(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));
}
101
Compare two String using compareTo() method
void main() {
String str1 = "a";
print(str1.compareTo("A"));
print(str1.compareTo("z"));
print(str1.compareTo("a"));
}
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());
}
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());
}
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());
}
Dart Tutorial
Happy Coding 😊
Related Articles
Dart integer Data Type by Examples
Dart double Data Type by Examples
Dart bool Data Type by Examples