![]() |
VOOZH | about |
A Dart string is a sequence of UTF-16 code units. With the same rule as that of Python, you can use either single or double quotes to create a string. The string starts with the datatype String or Var :
String string = "I love GeeksforGeeks";
var string1 = 'GeeksforGeeks is a great platform for upgrading skills';
Both the strings above when running on a Dart editor will work perfectly.
You can put the value of an expression inside a string by using ${expression}. It will help the strings to concatenate easily. If the expression is an identifier, you can skip the {}.
Output :
I do coding on Geeks for Geeks.
Dart also allows us to concatenate the string by + operator as well as we can just separate the two strings by Quotes. The concatenation also works over line breaks which is itself a very useful feature.
Output :
GeeksforGeeks
Coding is Fun
We can also check whether two strings are equal by == operator. It compares every element of the first string with every element of the second string.
Output :
TrueRaw strings are useful when you want to define a String that has a lot of special characters. We can create a raw string by prefixing it with r .
Output:
This is a raw stringProperty | Description |
|---|---|
length | Returns the number of characters in the string. |
isEmpty | Returns |
isNotEmpty | Returns |
Implementation of Properties:
Output:
3
false
true
Method | Description |
|---|---|
toLowerCase() | Converts all characters to lowercase. |
toUpperCase() | Converts all characters to uppercase. |
trim() | Removes leading and trailing whitespaces. |
trimLeft() | Removes leading whitespaces. |
trimRight() | Removes trailing whitespaces. |
padLeft(width, [padding]) | Adds padding to the left to reach a given width. |
padRight(width, [padding]) | Adds padding to the right to reach a given width. |
contains(pattern) | Checks if the string contains a given pattern. |
startsWith(pattern, [index]) | Checks if the string starts with a given pattern. |
endsWith(pattern) | Checks if the string ends with a given pattern |
indexOf(pattern, [start]) | Returns the first index of the pattern. |
lastIndexOf(pattern, [start]) | Returns the last index of the pattern. |
replaceFirst(from, to, [start]) | Replaces the first occurrence of |
replaceAll(from, to) | Replaces all occurrences of |
replaceRange(start, end, replacement) | Replaces a range of characters with another string |
split(pattern) | Splits the string into a list of substrings |
substring(start, [end]) | Extracts a portion of the string. |
codeUnitAt(index) | Returns the Unicode code unit at the given index. |
compareTo(other) | Compares this string with another string. |
toString() | Returns the string itself. |
r'...' | Creates a raw string that ignores escape characters. |
Implementation of methods :
Output:
Lowercase: dart programming
Uppercase: DART PROGRAMMING
Trimmed: "Dart Programming"
Padded Left: "***** Dart Programming "
Padded Right: " Dart Programming *****"
Contains "Dart": true
Starts with " Dart": true
Ends with "ing ": true
Index of "Dart": 2
Last Index of "g": 17
Replace First "Dart" with "Flutter": Flutter Programming
Replace All " " with "-": --Dart-Programming--
Split by space: [Dart, Programming]
Substring (2 to 6): Dart
Unicode at index 2: 68
Compare "Dart" & "Programming": -1
To String: Dart Programming
Raw String: This is a \n raw string
Dart strings offer a powerful and flexible way to manage text data. They support both single and double quotes, as well as string interpolation for efficient concatenation. Dart provides various built-in methods for string manipulation, ensuring efficient string operations through properties like length, isEmpty, and isNotEmpty. Key methods include toUpperCase(), replaceAll(), split(), and substring().
Moreover, Dart allows the use of raw strings, which enable the handling of special characters without needing escape sequences. It also supports the definition of multiline strings. A solid understanding of these features helps developers effectively process and format text-based data in Dart applications.