Methods Of String

في لغة Dart، تحتوي كل كائنات النصوص (String) على مجموعة من الدوال (Methods) المدمجة التي تتيح لك معالجة النصوص بسهولة. إليك شرحًا مفصلاً لأهم هذه الدوال مع أمثلة:


1. الخصائص الأساسية (Properties)

  • length: يُرجع طول النص (عدد الأحرف).dartCopyString text = “Dart”; print(text.length); // الناتج: 4
  • isEmptytrue إذا كان النص فارغًا.dartCopyprint(“”.isEmpty); // الناتج: true
  • isNotEmptytrue إذا كان النص غير فارغ.dartCopyprint(“Hello”.isNotEmpty); // الناتج: true

2. تحويل الحروف (Case Conversion)

  • toUpperCase(): يحول النص إلى أحرف كبيرة.dartCopyprint(“dart”.toUpperCase()); // الناتج: “DART”
  • toLowerCase(): يحول النص إلى أحرف صغيرة.dartCopyprint(“DART”.toLowerCase()); // الناتج: “dart”

3. البحث والتفحص (Searching & Checking)

  • contains(): يتحقق من وجود جزء من النص.dartCopyString text = “Hello World”; print(text.contains(“World”)); // الناتج: true
  • startsWith(): يتحقق إذا بدأ النص بجزء معين.dartCopyprint(text.startsWith(“Hello”)); // الناتج: true
  • endsWith(): يتحقق إذا انتهى النص بجزء معين.dartCopyprint(text.endsWith(“World”)); // الناتج: true
  • indexOf(): يُرجع مؤشر أول ظهور لجزء من النص.dartCopyprint(text.indexOf(“o”)); // الناتج: 4
  • lastIndexOf(): يُرجع مؤشر آخر ظهور لجزء من النص.dartCopyprint(text.lastIndexOf(“o”)); // الناتج: 7

4. التعديل (Modification)

  • trim(): يزيل المسافات من الطرفين.dartCopyprint(” Dart “.trim()); // الناتج: “Dart”
  • trimLeft(): يزيل المسافات من الطرف الأيسر.dartCopyprint(” Dart”.trimLeft()); // الناتج: “Dart”
  • trimRight(): يزيل المسافات من الطرف الأيمن.dartCopyprint(“Dart “.trimRight()); // الناتج: “Dart”
  • replaceAll(): يستبدل كل التطابقات.dartCopyprint(“apple,banana,apple”.replaceAll(“apple”, “orange”)); // الناتج: “orange,banana,orange”
  • replaceFirst(): يستبدل أول تطابق فقط.dartCopyprint(“apple,banana,apple”.replaceFirst(“apple”, “orange”)); // الناتج: “orange,banana,apple”
  • replaceRange(): يستبدل جزءًا محددًا من النص.dartCopyprint(“Dart is fun”.replaceRange(5, 7, “was”)); // الناتج: “Dart was fun”
  • padLeft(): يضيف أحرفًا إلى اليسار حتى يصل النص إلى طول محدد.dartCopyprint(“5”.padLeft(3, “0”)); // الناتج: “005”
  • padRight(): يضيف أحرفًا إلى اليمين حتى يصل النص إلى طول محدد.dartCopyprint(“5”.padRight(3, “0”)); // الناتج: “500”

5. التقسيم والدمج (Splitting & Joining)

  • split(): يقسم النص إلى قائمة (List) بناءً على محدد.dartCopyprint(“apple,banana,orange”.split(“,”)); // الناتج: [“apple”, “banana”, “orange”]
  • splitMapJoin(): يقسم النص، يعالج كل جزء، ثم يدمجه.dartCopyString result = “Hello World”.splitMapJoin(” “, onMatch: (m) => “*”, onNonMatch: (n) => n.toUpperCase() ); print(result); // الناتج: “HELLO*WORLD”

6. الاستخراج (Extraction)

  • substring(): يستخرج جزءًا من النص.dartCopyString text = “Hello Dart”; print(text.substring(6)); // الناتج: “Dart” print(text.substring(0, 5)); // الناتج: “Hello”
  • codeUnitAt(): يُرجع رمز Unicode للحرف في موضع محدد.dartCopyprint(“Dart”.codeUnitAt(0)); // الناتج: 68 (رمز ‘D’ في Unicode)

7. المقارنة (Comparison)

  • compareTo(): يقارن نصين (يعيد 0 إذا كانا متساويين).dartCopyprint(“apple”.compareTo(“banana”)); // الناتج: -1 (لأن “apple” تأتي قبل “banana”)

8. التحويل إلى قائمة (Conversion to List)

  • **runes: تحويل النص إلى قائمة من رموز Unicode.dartCopyString emoji = “😊”; print(emoji.runes.toList()); // الناتج: [128522]

9. أمثلة متقدمة

مثال 1: تحويل أول حرف إلى كبير

dart

Copy

String capitalize(String text) {
  if (text.isEmpty) return "";
  return text[0].toUpperCase() + text.substring(1).toLowerCase();
}
print(capitalize("dart")); // الناتج: "Dart"

مثال 2: عكس النص

dart

Copy

String reverse(String text) {
  return text.split('').reversed.join();
}
print(reverse("Dart")); // الناتج: "traD"

ملاحظات مهمة

  • النصوص في Dart غير قابلة للتعديل (immutable)، أي أن أي عملية تعديل تُنشئ نصًا جديدًا.
  • معظم الدوال حساسة لحالة الأحرف (Case-Sensitive).
  • يمكن استخدام التعبيرات المنتظمة (RegExp) مع دوال مثل contains() أو replaceAll().dartCopyprint(“Dart3”.contains(RegExp(r'[0-9]’))); // الناتج: true

الاستخدام مع القوالب النصية (String Interpolation)

dart

Copy

String name = "Ahmed";
int age = 25;
print("الاسم: $name, العمر: ${age}"); // الناتج: "الاسم: Ahmed, العمر: 25"

النصوص متعددة الأسطر

استخدم ثلاث علامات اقتباس (''' أو """):

dart

Copy

String multiLine = '''
  هذا نص
  متعدد
  الأسطر.
''';