Master string manipulation and text processing in Dart
A string is a sequence of characters. In Dart, strings can be created with single or double quotes.
``darString single = 'Hello';
String double = "World";
String multiline = '''
This is a
multiline string
''';
`Insert variables directly into strings using `$variable` or `${expression}`.
``darString name = 'Alice';
int age = 25;
print('Name: $name');
print('Age: $age');
print('Next year: ${age + 1}');
print('Upper: ${name.toUpperCase()}');
`Get the number of characters.
``darString text = 'Hello';
print(text.length); // 5
`Check if string is empty.
``darString text = '';
print(text.isEmpty); // true
print(text.isNotEmpty); // false
```darString text = 'Hello World';
print(text.toUpperCase()); // HELLO WORLD
print(text.toLowerCase()); // hello world
`Extract part of a string.
``darString text = 'Hello World';
print(text.substring(0, 5)); // Hello
print(text.substring(6)); // World
`Check if string contains a substring.
``darString text = 'Hello World';
print(text.contains('World')); // true
print(text.contains('Dart')); // false
```darString text = 'Hello World';
print(text.startsWith('Hello')); // true
print(text.endsWith('World')); // true
`Find position of substring.
``darString text = 'Hello World';
print(text.indexOf('o')); // 4
print(text.indexOf('World')); // 6
`Split string into list of substrings.
``darString csv = 'apple,banana,orange';
List
print(fruits); // [apple, banana, orange]
`Replace occurrences of substring.
``darString text = 'Hello Hello Hello';
print(text.replaceAll('Hello', 'Hi')); // Hi Hi Hi
print(text.replaceFirst('Hello', 'Hi')); // Hi Hello Hello
`Remove whitespace.
``darString text = ' Hello World ';
print('trim: "|${text.trim()}|"'); // Hello World
print('trimLeft: "|${text.trimLeft()}|"'); // Hello World
print('trimRight: "|${text.trimRight()}|"'); // Hello World
`Add padding.
``darString text = 'Hi';
print(text.padLeft(5, '*')); // ***Hi
print(text.padRight(5, '*')); // Hi***
`Reverse a string.
``darString text = 'Hello';
String reversed = String.fromCharCodes(text.codeUnits.reversed);
print(reversed); // olleH
`Get ASCII/Unicode value of character.
``darString text = 'A';
print(text.codeUnitAt(0)); // 65
```darString text = 'Dart';
for (int i = 0; i < text.length; i++) {
print(text[i]); // D, a, r, t
}
for (var char in text.split('')) {
print(char); // D, a, r, t
}
`1. **Use String Interpolation**: Prefer `'Value: $value'` over concatenation
2. **Check isEmpty**: Before using strings
3. **Trim Input**: Remove unwanted whitespace
4. **Validate Format**: Check string format before processing
5. **Handle Special Characters**: Escape quotes and newlines
6. **Use Raw Strings**: For paths and regex with `r'path\file'`
7. **Consider Null Safety**: Strings can be String? (nullable)
``dar// Email validation
bool isEmail(String email) {
return email.contains('@');
}
// Remove duplicates
String removeDuplicates(String text) {
return text.split('').toSet().join();
}
// Capitalize first letter
String capitalize(String text) {
if (text.isEmpty) return text;
return text[0].toUpperCase() + text.substring(1);
}
`