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 ****{}**.
Dart `
void main() {
// Declare a string variable and assign a value
var string = 'I do coding';
// Use string interpolation to combine 'string' with another string
var string1 = '$string on Geeks for Geeks';
// Print the final concatenated string
print(string1);
}
`
**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.
Dart ``
void main() {
// Declare a string variable by concatenating
// multiple string literals
var string = 'Geeks' 'for' 'Geeks';
// Dart allows implicit concatenation
// of adjacent string literals
// Declare two separate string variables
var str = 'Coding is ';
var str1 = 'Fun';
// Print the concatenated string
// using implicit concatenation
print(string);
// Output: GeeksforGeeks
// Print the concatenated string using the `+` operator
print(str + str1);
// Output: Coding is Fun
}
``
**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.
Dart `
void main() {
// Declare two string variables with the same value
var str = 'Geeks';
var str1 = 'Geeks';
// Check if both strings are equal
if (str == str1) {
print('True');
// Output: True (because both strings are identical)
}
}
`
**Output :
True
**Raw 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 .
Dart `
void main() {
// Declaring a raw string using 'r'
// before the string literal
var gfg = r'This is a raw string';
// Printing the raw string
print(gfg);
}
`
**Output:
This is a raw string
Property | Description |
---|---|
length | Returns the number of characters in the string. |
isEmpty | Returns true if the string is empty. |
isNotEmpty | Returns true if the string is not empty. |
**Implementation of Properties:
Dart `
void main() { var str = "GFG"; print(str.length); // 4 print(str.isEmpty); // false print(str.isNotEmpty); // true }
`
**Output:
3
false
true
Methods of String in Dart
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 from with to. |
replaceAll(from, to) | Replaces all occurrences of from with to. |
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 :
Dart `
void main() { var str = ' Dart Programming '; var str1 = 'Dart'; var str2 = 'Programming';
// toLowerCase - Converts to lowercase
print('Lowercase: ${str.toLowerCase()}');
// Output: dart programming
// toUpperCase - Converts to uppercase
print('Uppercase: ${str.toUpperCase()}');
// Output: DART PROGRAMMING
// trim - Removes leading and trailing spaces
print('Trimmed: "${str.trim()}"');
// Output: "Dart Programming"
// padLeft - Adds padding to the left
print('Padded Left: "${str.padLeft(25, '*')}"');
// Output: "**** Dart Programming "
// padRight - Adds padding to the right
print('Padded Right: "${str.padRight(25, '*')}"');
// Output: " Dart Programming ****"
// contains - Checks if a string contains another string
print('Contains "Dart": ${str.contains("Dart")}');
// Output: true
// startsWith - Checks if a string starts with a given substring
print('Starts with " Dart": ${str.startsWith(" Dart")}');
// Output: true
// endsWith - Checks if a string ends with a given substring
print('Ends with "ing ": ${str.endsWith("ing ")}');
// Output: true
// indexOf - Returns index of first occurrence of substring
print('Index of "Dart": ${str.indexOf("Dart")}');
// Output: 2
// lastIndexOf - Returns last occurrence index
print('Last Index of "g": ${str.lastIndexOf("g")}'); // Output: 18
// replaceFirst - Replaces first occurrence
print('Replace First "Dart" with "Flutter": ${str.replaceFirst("Dart", "Flutter")}');
// Output: " Flutter Programming "
// replaceAll - Replaces all occurrences
print('Replace All " " with "-": ${str.replaceAll(" ", "-")}');
// Output: "--Dart-Programming--"
// split - Splits string into list
print('Split by space: ${str.trim().split(" ")}');
// Output: [Dart, Programming]
// substring - Extracts a part of the string
print('Substring (2 to 6): ${str.substring(2, 6)}');
// Output: Dart
// codeUnitAt - Returns Unicode unit at index
print('Unicode at index 2: ${str.codeUnitAt(2)}');
// Output: 68 (D)
// compareTo - Compares two strings
print('Compare "Dart" & "Programming": ${str1.compareTo(str2)}');
// Output: -1
// toString - Converts to string
print('To String: ${str.toString()}');
// Output: " Dart Programming "
// Raw String
var rawStr = r'This is a \n raw string';
print('Raw String: $rawStr');
// Output: This is a \n raw string
}
`
**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.