Bỏ qua để đến nội dung

Chuỗi ký tự (String)

🎯 Mục tiêu: Nắm vững String operations và String Interpolation.


String single = 'Single quotes';
String double = "Double quotes";
String multi = '''
Multi-line
string
''';
String raw = r'Raw: \n not escaped';

var name = "Dart";
var version = 3;
print("$name version $version");
print("Length: ${name.length}");
print("Upper: ${name.toUpperCase()}");

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

// Concatenation
var s1 = "Hello" + " " + "Dart";
// StringBuffer (efficient for many appends)
var buffer = StringBuffer();
buffer.write("Hello ");
buffer.write("Dart");
print(buffer.toString());

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");
}

  • Tạo string với ', ", '''
  • Sử dụng String Interpolation $${}
  • Sử dụng các methods: trim, split, contains

Tiếp theo: String Interpolation