String Operations

Master string manipulation and text processing in Dart

String Operations in Dart


What is a String?


A string is a sequence of characters. In Dart, strings can be created with single or double quotes.


``dar

String single = 'Hello';

String double = "World";

String multiline = '''

This is a

multiline string

''';

`

String Interpolation


Insert variables directly into strings using `$variable` or `${expression}`.


``dar

String name = 'Alice';

int age = 25;


print('Name: $name');

print('Age: $age');

print('Next year: ${age + 1}');

print('Upper: ${name.toUpperCase()}');

`

String Properties


length

Get the number of characters.


``dar

String text = 'Hello';

print(text.length); // 5

`

isEmpty / isNotEmpty

Check if string is empty.


``dar

String text = '';

print(text.isEmpty); // true

print(text.isNotEmpty); // false

`
Cell2Dart
// String properties and basic operations String greeting = 'Hello, Dart!'; print('Original: $greeting'); print('Length: ${greeting.length}'); print('Is empty: ${greeting.isEmpty}'); print('Is not empty: ${greeting.isNotEmpty}'); // String concatenation String first = 'Hello'; String second = 'World'; String combined = first + ' ' + second; print('Combined: $combined'); // Using + operator String result = 'Dart ' + 'is ' + 'awesome'; print(result);

String Methods


Case Conversion


``dar

String text = 'Hello World';


print(text.toUpperCase()); // HELLO WORLD

print(text.toLowerCase()); // hello world

`

Substring


Extract part of a string.


``dar

String text = 'Hello World';


print(text.substring(0, 5)); // Hello

print(text.substring(6)); // World

`

contains()


Check if string contains a substring.


``dar

String text = 'Hello World';


print(text.contains('World')); // true

print(text.contains('Dart')); // false

`

startsWith() / endsWith()


``dar

String text = 'Hello World';


print(text.startsWith('Hello')); // true

print(text.endsWith('World')); // true

`

indexOf()


Find position of substring.


``dar

String text = 'Hello World';


print(text.indexOf('o')); // 4

print(text.indexOf('World')); // 6

`
Cell4Dart
// String manipulation methods String text = 'Hello World'; // Case conversion print('Upper: ${text.toUpperCase()}'); print('Lower: ${text.toLowerCase()}'); // Substring extraction print('First 5: ${text.substring(0, 5)}'); print('After space: ${text.substring(6)}'); // Search methods print('Contains World: ${text.contains('World')}'); print('Starts with Hello: ${text.startsWith('Hello')}'); print('Ends with World: ${text.endsWith('World')}'); // Find position print('Index of o: ${text.indexOf('o')}'); print('Index of World: ${text.indexOf('World')}');

More String Methods


split()


Split string into list of substrings.


``dar

String csv = 'apple,banana,orange';

List fruits = csv.split(',');

print(fruits); // [apple, banana, orange]

`

replaceAll() / replaceFirst()


Replace occurrences of substring.


``dar

String text = 'Hello Hello Hello';


print(text.replaceAll('Hello', 'Hi')); // Hi Hi Hi

print(text.replaceFirst('Hello', 'Hi')); // Hi Hello Hello

`

trim() / trimLeft() / trimRight()


Remove whitespace.


``dar

String text = ' Hello World ';


print('trim: "|${text.trim()}|"'); // Hello World

print('trimLeft: "|${text.trimLeft()}|"'); // Hello World

print('trimRight: "|${text.trimRight()}|"'); // Hello World

`

padLeft() / padRight()


Add padding.


``dar

String text = 'Hi';


print(text.padLeft(5, '*')); // ***Hi

print(text.padRight(5, '*')); // Hi***

`

reverse (via runes)


Reverse a string.


``dar

String text = 'Hello';

String reversed = String.fromCharCodes(text.codeUnits.reversed);

print(reversed); // olleH

`
Cell6Dart
// Advanced string operations String sentence = 'The quick brown fox jumps'; // Split into words List<String> words = sentence.split(' '); print('Words: $words'); // Replace text String modified = sentence.replaceAll('quick', 'slow'); print('Modified: $modified'); String replacedOnce = sentence.replaceFirst('o', '0'); print('Replaced first: $replacedOnce'); // Trim whitespace String padded = ' hello '; print('Trimmed: |${padded.trim()}|'); print('Padded left: |${padded.trim().padLeft(12, '*')}|'); print('Padded right: |${padded.trim().padRight(12, '*')}|');

Working with Characters


codeUnitAt()


Get ASCII/Unicode value of character.


``dar

String text = 'A';

print(text.codeUnitAt(0)); // 65

`

Iterating Over Characters


``dar

String 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

}

`

String Best Practices


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)


Common String Patterns


``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);

}

`
Run code to see string operations in action