Strings 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β
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β
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β
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.
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.
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.
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.
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.
ToString In Dartβ
In dart, toString()
represents String representation of the value/object.
SubString In Dartβ
When you want to get text from any position then you can use substring in dart.
Reverse String In Dartβ
If you want to reverse string in dart, you can reverse it by using different solution. One solution is here.