Dart Strings

Working with text data in Dart

📝 What are Dart Strings?

Strings in Dart represent text data. They are sequences of characters enclosed in single or double quotes, used for storing and manipulating text in your applications.


// Simple string examples
String name = 'Alice';
String message = "Hello, World!";
print(name); // Output: Alice
                                    

String Creation Methods

''

Single Quotes

Create strings with single quotes

String text = 'Hello Dart';
""

Double Quotes

Create strings with double quotes

String text = "Hello Dart";
🔗

String Interpolation

Embed variables in strings

String name = 'Bob';
String greeting = 'Hi $name!';
📄

Multi-line Strings

Create strings spanning multiple lines

String poem = '''
Line one
Line two
Line three
''';

🔹 Basic String Operations

Common operations you can perform with strings:

String firstName = 'John';
String lastName = 'Doe';

// String concatenation
String fullName = firstName + ' ' + lastName;
print(fullName); // Output: John Doe

// String length
print(firstName.length); // Output: 4

// Convert to uppercase/lowercase
print(firstName.toUpperCase()); // Output: JOHN
print(lastName.toLowerCase()); // Output: doe

Output:

John Doe
4
JOHN
doe

🔹 String Interpolation

Embed variables and expressions directly in strings:

String name = 'Alice';
int age = 25;
double height = 5.6;

// Simple interpolation
String intro = 'My name is $name';
print(intro);

// Expression interpolation
String details = 'I am ${age + 5} years old in 5 years';
print(details);

// Multiple variables
String info = '$name is $age years old and ${height}ft tall';
print(info);

Output:

My name is Alice
I am 30 years old in 5 years
Alice is 25 years old and 5.6ft tall

🔹 String Methods

Useful methods for string manipulation:

String text = '  Hello Dart World  ';

// Remove whitespace
print(text.trim()); // Output: Hello Dart World

// Check if string contains substring
print(text.contains('Dart')); // Output: true

// Replace text
print(text.replaceAll('Dart', 'Flutter')); // Output:   Hello Flutter World  

// Split string into list
List<String> words = text.trim().split(' ');
print(words); // Output: [Hello, Dart, World]

// Check if string starts/ends with
print(text.trim().startsWith('Hello')); // Output: true
print(text.trim().endsWith('World')); // Output: true

Output:

Hello Dart World
true
Hello Flutter World
[Hello, Dart, World]
true
true

🔹 Multi-line Strings

Create strings that span multiple lines:

// Using triple quotes
String multiLine = '''
This is line one
This is line two
This is line three
''';
print(multiLine);

// Raw strings (ignore escape sequences)
String rawString = r'This is a raw string with \n and \t';
print(rawString);

// String with escape sequences
String escaped = 'Line 1\nLine 2\tTabbed';
print(escaped);

Output:

This is line one
This is line two
This is line three

This is a raw string with \n and \t

Line 1
Line 2    Tabbed

🧠 Test Your Knowledge

Which symbol is used for string interpolation in Dart?