String Methods
Strings in Dart come with a powerful set of built-in methods that make text manipulation easy and efficient. Think of these methods as your toolkit for working with text - whether you need to transform, search, or extract parts of a string.
Common String Methods
Let's explore the most frequently used string methods with practical examples:
Length and Basic Properties
String message = "Hello, Dart!";
print(message.length); // Output: 12
print(message.isEmpty); // Output: false
print(message.isNotEmpty); // Output: true
String empty = "";
print(empty.isEmpty); // Output: true
Case Conversion
String text = "Hello World";
print(text.toLowerCase()); // Output: hello world
print(text.toUpperCase()); // Output: HELLO WORLD
Searching and Checking
String sentence = "Dart is awesome";
// Check if string contains substring
print(sentence.contains("Dart")); // Output: true
print(sentence.contains("Python")); // Output: false
// Find position of substring
print(sentence.indexOf("is")); // Output: 5
print(sentence.indexOf("xyz")); // Output: -1 (not found)
// Check start and end
print(sentence.startsWith("Dart")); // Output: true
print(sentence.endsWith("awesome")); // Output: true
Extracting Parts of Strings
String text = "Programming";
// Get substring
print(text.substring(0, 7)); // Output: Program
print(text.substring(7)); // Output: ming
// Get characters at specific positions
print(text[0]); // Output: P
print(text[text.length - 1]); // Output: g
Splitting and Joining
String fruits = "apple,banana,orange";
List<String> fruitList = fruits.split(",");
print(fruitList); // Output: [apple, banana, orange]
// Join list back to string
String joined = fruitList.join(" - ");
print(joined); // Output: apple - banana - orange
Trimming Whitespace
String messy = " Hello World ";
print(messy.trim()); // Output: Hello World
print(messy.trimLeft()); // Output: Hello World
print(messy.trimRight()); // Output: Hello World
Replacing Text
String original = "I love Java programming";
String updated = original.replaceAll("Java", "Dart");
print(updated); // Output: I love Dart programming
// Replace only first occurrence
String text = "cat and cat";
print(text.replaceFirst("cat", "dog")); // Output: dog and cat
Method Chaining
You can chain multiple string methods together for powerful text processing:
String input = " HELLO WORLD ";
String result = input.trim().toLowerCase().replaceAll(" ", "_");
print(result); // Output: hello_world
Practical Example
Here's a real-world example of using string methods to format a user's name:
String rawName = " jOhN dOe ";
String formattedName = rawName
.trim() // Remove extra spaces
.toLowerCase() // Convert to lowercase
.split(" ") // Split into parts
.map((part) => part[0].toUpperCase() + part.substring(1)) // Capitalize each part
.join(" "); // Join back together
print(formattedName); // Output: John Doe
Quick Reference Table
| Method | Purpose | Example |
|---|---|---|
length | Get string length | "Hello".length → 5 |
isEmpty | Check if empty | "".isEmpty → true |
toLowerCase() | Convert to lowercase | "HI".toLowerCase() → "hi" |
toUpperCase() | Convert to uppercase | "hi".toUpperCase() → "HI" |
contains() | Check if contains substring | "hello".contains("ell") → true |
indexOf() | Find position of substring | "hello".indexOf("l") → 2 |
substring() | Extract part of string | "hello".substring(1, 4) → "ell" |
split() | Split into list | "a,b,c".split(",") → ["a", "b", "c"] |
trim() | Remove whitespace | " hi ".trim() → "hi" |
replaceAll() | Replace all occurrences | "hi hi".replaceAll("hi", "bye") → "bye bye" |
These string methods are your best friends when working with text in Dart. They help you clean, transform, and manipulate strings efficiently!