String in Dart
String In Dart
String data types help you to store text data. In string, you can represent your name, address, or even the entire complete text of the book. It is used to hold a series or sequence of characters – letters, numbers, and special characters. You can use single or double or triple quotes to represent string.
Example
void main() {
String text1 = 'This is an example of a single-line string.';
String text2 = "This is an example of a double-quotes multiline line string.";
String text3 = """This is a multiline line
string using the triple-quotes.""";
print(text1);
print(text2);
print(text3);
}
String Concatenation
You can combine one string with another string. This is called concatenation. In Dart, you can use the +
operator or use interpolation
to concatenate the string. Interpolation makes it easy to read and understand the code.
Example
void main() {
String firstName = "John";
String lastName = "Doe";
print("Using +, Full Name is "+firstName + " " + lastName.);
print("Using interpolation, fulname is $firstName $lastName.");
}
Properties Of String
- codeUnits: Returns an unmodifiable list of the UTF-16 code units of this string.
- isEmpty: Returns true if this string is empty.
- isNotEmpty: Returns false if this string is empty.
- Length: Returns the length of the string including space, tab, and newline characters.
String Properties Example In Dart
void main() {
String str = "Hi";
print(str.codeUnits); //Example of code units
print(str.isEmpty); //Example of isEmpty
print(str.isNotEmpty); //Example of isNotEmpty
print("The length of the string is: ${str.length}"); //Example of Length
}
Methods Of String
- toLowerCase(): Converts all characters in this string to lowercase.
- toUpperCase(): Converts all characters in this string to uppercase.
- trim(): Returns the string without any leading and trailing whitespace.
- compareTo(): Compares this object to another.
- replaceAll(): Replaces all substrings that match the specified pattern with a given value.
- split(): Splits the string at matches of the specified delimiter and returns a list of substrings.
- toString(): Returns a string representation of this object.
- substring(): Returns the substring of this string that extends from startIndex, inclusive, to endIndex, exclusive.
- codeUnitAt(): Returns the 16-bit UTF-16 code unit at the given index.
String Methods Example In Dart
Here you will see various methods example, that can help your work a lot better and fast.
Converting String To Uppercase and Lowercase
You can convert your text to lower case using .toLowerCase() and convert to uppercase using .toUpperCase() method.
//Example of toUpperCase() and toLowerCase()
void main() {
String address1 = "Florida"; // Here F is capital
String address2 = "TexAs"; // Here T and A are capital
print("Address 1 in uppercase: ${address1.toUpperCase()}");
print("Address 1 in lowercase: ${address1.toLowerCase()}");
print("Address 2 in uppercase: ${address2.toUpperCase()}");
print("Address 2 in lowercase: ${address2.toLowerCase()}");
}
Trim String In Dart
Trim is helpful when removing leading and trailing spaces from the text. This trim method will remove all the starting and ending spaces from the text.
Note: In dart, trim() method doesn’t remove spaces in the middle.
//Example of trim()
void main() {
String address1 = " USA"; // Contain space at leading.
String address2 = "Japan "; // Contain space at trailing.
String address3 = "New Delhi"; // Contains space at middle.
print("Result of address1 trim is ${address1.trim()}");
print("Result of address2 trim is ${address2.trim()}");
print("Result of address3 trim is ${address3.trim()}");
}
Compare String In Dart
In dart, you can compare two strings. It will give result 0 when two texts are equal, 1 when the first string is greater than the second, and -1 when the first string is smaller than the second.
//Example of compareTo()
void main() {
String item1 = "Apple";
String item2 = "Ant";
String item3 = "Basket";
print("Comparing item 1 with item 2: ${item1.compareTo(item2)}");
print("Comparing item 1 with item 3: ${item1.compareTo(item3)}");
print("Comparing item 3 with item 2: ${item3.compareTo(item2)}");
}
Replace String In Dart
You can replace one value with another with the replaceAll(“old”, “new”) method in dart. It will replace all the “old” words with “new”. Here in this example, this will replace milk with water.
//Example of replaceAll()
void main() {
String text = "I am a good boy I like milk. Doctor says milk is good for health.";
String newText = text.replaceAll("milk", "water");
print("Original Text: $text");
print("Replaced Text: $newText");
}
Split String In Dart
If you want to split String by comma, space, or other text, you can use the dart split method. It will help you to split String to list.
//Example of split()
void main() {
String allNames = "Ram, Hari, Shyam, Gopal";
List<String> listNames = allNames.split(",");
print("Value of listName is $listNames");
print("List name at 0 index ${listNames[0]}");
print("List name at 1 index ${listNames[1]}");
print("List name at 2 index ${listNames[2]}");
print("List name at 3 index ${listNames[3]}");
}
ToString In Dart
In dart, toString() represents String representation of the value/object.
//Example of toString()
void main() {
int number = 20;
String result = number.toString();
print("Type of number is ${number.runtimeType}");
print("Type of result is ${result.runtimeType}");
}
SubString In Dart
When you want to get text from any position then you can use substring in dart.
//Example of substring()
void main() {
String text = "I love computer";
print("Print only computer: ${text.substring(7)}"); // from index 6 to the last index
print("Print only love: ${text.substring(2,6)}");// from index 2 to the 6th index
}
Reverse String In Dart
If you want to reverse string in dart, you can reverse it by using different solution. One solution is here.
void main() {
String input = "Hello";
print("$input Reverse is ${input.split('').reversed.join()}");
}