Strings in Dart
In this lesson, you will learn about Strings in Dart. Strings are used to store and manipulate text such as names, emails, messages, API responses, file content, and user inputs.
In real-world applications, strings appear everywhere — from login systems to reports, dashboards, and mobile apps.
What Is a String?
A string is a sequence of characters enclosed in single quotes
(' ') or double quotes (" ").
String name = "Dataplexa";
String country = 'USA';
Both single and double quotes work the same in Dart.
Real-World Example Dataset
We will use user profile information for practical examples.
String firstName = "John";
String lastName = "Doe";
String email = "john.doe@dataplexa.com";
String Interpolation
String interpolation allows you to insert variables directly into strings
using $ and ${}.
String fullName = "$firstName $lastName";
print("User Name: $fullName");
Interpolation is preferred over concatenation because it is cleaner and safer.
String Concatenation
You can also join strings using the + operator.
String message = "Welcome " + firstName + "!";
print(message);
However, interpolation is recommended in modern Dart code.
Common String Properties
Dart provides useful properties to analyze strings.
print(email.length);
print(email.isEmpty);
print(email.isNotEmpty);
Converting Case
You can convert strings to uppercase or lowercase.
print(firstName.toUpperCase());
print(lastName.toLowerCase());
This is commonly used in data normalization.
Checking Substrings
The contains() method checks if a string includes another string.
bool isCompanyEmail = email.contains("dataplexa");
print(isCompanyEmail);
Starts With & Ends With
These methods are useful for validation logic.
print(email.startsWith("john"));
print(email.endsWith(".com"));
Replacing Text
The replaceAll() method replaces part of a string.
Example: Mask email domain.
String maskedEmail = email.replaceAll("dataplexa.com", "*****.com");
print(maskedEmail);
Splitting Strings
The split() method converts a string into a list.
Example: Extract username and domain.
var parts = email.split("@");
print(parts);
print("Username: ${parts[0]}");
print("Domain: ${parts[1]}");
Trimming Spaces
Use trim() to remove extra spaces.
String rawInput = " Dart Programming ";
print(rawInput.trim());
This is critical for handling user inputs.
Accessing Characters
You can access characters using index positions.
print(firstName[0]);
print(lastName[1]);
Indexes start from 0.
Real-World Use Cases
- User name formatting
- Email validation
- Search filters
- Data cleaning
- API response processing
📝 Practice Exercises
Exercise 1
Create a string and print its length.
Exercise 2
Check if a string contains a specific word.
Exercise 3
Split an email address into username and domain.
✅ Practice Answers
Answer 1
String course = "Dart Programming";
print(course.length);
Answer 2
String text = "Learning Dart is fun";
print(text.contains("Dart"));
Answer 3
String mail = "user@example.com";
var data = mail.split("@");
print(data);
What’s Next?
In the next lesson, you will learn about Type Conversion in Dart, including converting between strings, numbers, and other data types.