String Properties
Following are the properties supported by Strings in Dart:
isEmpty
It returns true in case the String is empty. For example,
void main() {
String str1 = "";
String str2 = "Not empty";
print("Is str1 empty? ${str1.isEmpty}");
print("Is str2 empty? ${str2.isEmpty}");
}
Output:
Is str1 empty? true
Is str2 empty? false
Length
It returns the length of the String. For example,
void main() {
String str1 = "";
String str2 = "Not empty";
print("Length of str1: ${str1.length}");
print("Length of str2: ${str2.length}");
}
Output:
Length of str1: 0
Length of str2: 9
String Methods
Dart provides a lot of methods to manipulate the Strings, which are as follows:
substring()
This method is used to slice a string. Given a start and an end index, it will return the part of the string that lies between those indices. For example,
void main() {
String str = "Hello World!";
print("The Substring of str between 1 and 5 indices is: ${str.substring(1,5)}");
}
Output:
The Substring of str between 1 and 5 indices is: ello
toUpperCase()
It converts all the characters of the given string to upper case. For example,
void main() {
String str = "Hello World!";
print("The Upper case Representation of str: ${str.toUpperCase()}");
}
Output:
The Upper case Representation of str: HELLO WORLD!
toLowerCase()
It converts all the characters of the given string to lower case. For example,
void main() {
String str = "Hello World!";
print("The Lower case Representation of str: ${str.toLowerCase()}");
}
Output:
The Lower case Representation of str: hello world!
compareTo()
It is used to compare two strings. It returns -1 if str1 is lexicographically smaller than str2, and 1 if str1 is larger than str2. In case both strings are equal, it returns 0. For example,
void main() {
String str1 = "AA";
String str2 = "AB";
String str3 = "AA";
print("Str1 and Str2 comparison: ${str1.compareTo(str2)}");
print("Str2 and Str3 comparison: ${str2.compareTo(str3)}");
print("Str3 and Str1 comparison: ${str3.compareTo(str1)}");
}
Output:
Str1 and Str2 comparison: -1
Str2 and Str3 comparison: 1
Str3 and Str1 comparison: 0
trim()
It trims any trailing and leading spaces of a string. For example,
void main() {
String str1 = "AA ";
String str2 = "AB";
print("str1, str2: ${str1.trim()}, $str2");
}
Output:
str1, str2: AA, AB
As you can see from the output, the output has no white spaces after str1.
split()
This method is used to split a string by specifying a particular delimiter. For example,
void main() {
String str1 = "A,B,C,D";
var l = str1.split(",");
print(l);
}
Output:
[A, B, C, D]
Frequently Asked Questions
-
Can single quotes be used for strings in Dart?
Yes, Dart supports both types of quotes for strings. Like python, dart also provides the functionality of using either single or double quotes for strings.
-
Does dart support multiline strings?
Yes, Dart supports multiline strings. Multiline strings are enclosed within triple quotes.
-
Is the end index mandatory for the substring method?
No. In case the end index is not given to the method, it will return a substring starting from the start index to the end of the entire string.
Conclusion
In this article, we have extensively discussed the Strings in Dart. We hope that this blog has helped you enhance your knowledge regarding the Dart String, and to learn more about Dart Classes. And to learn in-depth about android development, check out our Android Development course on the Coding Ninjas website. Do upvote our blog to help other ninjas grow. Happy Coding!
Recommended problems -