Chuỗi ký tự (String)
🎯 Mục tiêu: Nắm vững String operations và String Interpolation.
💡 Tạo String
Phần tiêu đề “💡 Tạo String”String single = 'Single quotes';String double = "Double quotes";String multi = '''Multi-linestring''';String raw = r'Raw: \n not escaped';📝 String Interpolation
Phần tiêu đề “📝 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
Phần tiêu đề “🔧 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") // trues.startsWith(" H") // trues.endsWith(" ") // trues.replaceAll("Dart", "Flutter")s.split(" ") // ["", "", "Hello", "Dart", "", ""]s.substring(2, 7) // "Hello"s.indexOf("Dart") // 8s.length // 14s.isEmpty // false🎯 String Building
Phần tiêu đề “🎯 String Building”// Concatenationvar s1 = "Hello" + " " + "Dart";
// StringBuffer (efficient for many appends)var buffer = StringBuffer();buffer.write("Hello ");buffer.write("Dart");print(buffer.toString());🛠️ Thực hành
Phần tiêu đề “🛠️ Thực hành”void main() { var email = "USER@EXAMPLE.COM";
// TODO: Validate và normalize email}Lời giải:
void main() { var email = "USER@EXAMPLE.COM";
var normalized = email.trim().toLowerCase(); var isValid = normalized.contains("@") && normalized.contains(".");
print("Email: $normalized"); print("Valid: $isValid");}✅ Checklist
Phần tiêu đề “✅ 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