Chuỗi ký tự (String) trong Dart
🎯 Mục tiêu: Nắm vững String operations và String Interpolation.
💡 Tạo String
String single = 'Single quotes';
String double = "Double quotes";
String multi = '''
Multi-line
string
''';
String raw = r'Raw: \n not escaped';📝 String Interpolation
var name = "Dart";
var version = 3;
print("$name version $version");
print("Length: ${name.length}");
print("Upper: ${name.toUpperCase()}");🔧 Các phương thức phổ biến
var s = " Hello Dart ";
s.trim() // "Hello Dart"
s.toUpperCase() // " HELLO DART "
s.toLowerCase() // " hello dart "
s.contains("Dart") // true
s.startsWith(" H") // true
s.endsWith(" ") // true
s.replaceAll("Dart", "Flutter")
s.split(" ") // ["", "", "Hello", "Dart", "", ""]
s.substring(2, 7) // "Hello"
s.indexOf("Dart") // 8
s.length // 14
s.isEmpty // false🎯 String Building
// Concatenation
var s1 = "Hello" + " " + "Dart";
// StringBuffer (efficient for many appends)
var buffer = StringBuffer();
buffer.write("Hello ");
buffer.write("Dart");
print(buffer.toString());🛠️ Thực hành
void main() {
var email = "[email protected]";
// TODO: Validate và normalize email
}Lời giải:
void main() {
var email = "[email protected]";
var normalized = email.trim().toLowerCase();
var isValid = normalized.contains("@") &&
normalized.contains(".");
print("Email: $normalized");
print("Valid: $isValid");
}✅ Checklist
- Tạo string với
',",''' - Sử dụng String Interpolation
$và${} - Sử dụng các methods:
trim,split,contains
Tiếp theo: String Interpolation
Last updated on