Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

ตรวจสอบว่าอาเรย์มีสมาชิกตรงเงื่อนไขเฉพาะหรือไม่ใน C#

บทความนี้สาธิตวิธีตรวจสอบว่า Array มีสมาชิกที่ตรงตามเงื่อนไขเฉพาะหรือไม่ โดยใช้เมธอด Array.Exists() ร่วมกับ StartsWith() และ lambda expression ใน C#

ตัวอย่างที่ 1: ตรวจสอบคำนำหน้าของสตริงในอาเรย์

using System;

public class Demo
{
    public static void Main()
    {
        string[] products = { "Electronics", "Accessories", "Clothing", "Toys", "Clothing", "Furniture" };

        Console.WriteLine("Products list...");
        foreach (string s in products)
        {
            Console.WriteLine(s);
        }

        Console.WriteLine("\nOne or more products begin with the letter 'C'? = {0}",
            Array.Exists(products, ele => ele.StartsWith("C")));

        Console.WriteLine("One or more planets begin with 'D'? = {0}",
            Array.Exists(products, ele => ele.StartsWith("D")));

        Console.WriteLine("One or more products begin with the letter 'T'? = {0}",
            Array.Exists(products, ele => ele.StartsWith("T")));

        Console.WriteLine("One or more planets begin with 'E'? = {0}",
            Array.Exists(products, ele => ele.StartsWith("E")));
    }
}

ผลลัพธ์

Products list...
Electronics
Accessories
Clothing
Toys
Clothing
Furniture
One or more products begin with the letter 'C'? = True
One or more planets begin with 'D'? = False
One or more products begin with the letter 'T'? = True
One or more planets begin with 'E'? = True

ตัวอย่างที่ 2: ตรวจสอบทั้งค่าที่ตรงกันพอดีและคำนำหน้า

using System;

public class Demo
{
    public static void Main()
    {
        string[] products = { "Electronics", "Accessories", "Clothing", "Toys", "Clothing", "Furniture" };

        Console.WriteLine("Products list...");
        foreach (string s in products)
        {
            Console.WriteLine(s);
        }

        Console.WriteLine("Is the products Accessories in the array? = {0}",
            Array.Exists(products, ele => ele == "Accessories"));

        Console.WriteLine("Is the products Stationery in the array? = {0}",
            Array.Exists(products, ele => ele == "Stationery"));

        Console.WriteLine("\nOne or more products begin with the letter 'C'? = {0}",
            Array.Exists(products, ele => ele.StartsWith("C")));

        Console.WriteLine("One or more planets begin with 'D'? = {0}",
            Array.Exists(products, ele => ele.StartsWith("D")));

        Console.WriteLine("One or more products begin with the letter 'T'? = {0}",
            Array.Exists(products, ele => ele.StartsWith("T")));

        Console.WriteLine("One or more planets begin with 'E'? = {0}",
            Array.Exists(products, ele => ele.StartsWith("E")));
    }
}

ผลลัพธ์

Products list...
Electronics
Accessories
Clothing
Toys
Clothing
Furniture
Is the products Accessories in the array? = True
Is the products Stationery in the array? = False
One or more products begin with the letter 'C'? = True
One or more planets begin with 'D'? = False
One or more products begin with the letter 'T'? = True
One or more planets begin with 'E'? = True

สรุป

  • ใช้ Array.Exists<T>(T[] array, Predicate<T> match) เพื่อตรวจสอบว่ามีสมาชิกอย่างน้อย 1 ตัวตรงเงื่อนไขหรือไม่ — คืนค่า bool
  • Predicate<T> คือ delegate ที่รับ T คืน bool — เขียนง่ายด้วย lambda: x => condition
  • StartsWith(string) ตรวจสอบว่าสตริงเริ่มต้นด้วยข้อความที่ระบุหรือไม่ (case-sensitive)
  • เมธอด Array static อื่นๆ ที่น่าสนใจ:
    • Array.TrueForAll — ทุกตัวตรงเงื่อนไข
    • Array.Find — หาตัวแรกที่ตรง
    • Array.FindAll — หาทุกตัวที่ตรง (คืนอาเรย์)
    • Array.FindIndex / FindLastIndex — หาดัชนี
    • Array.BinarySearch — ค้นหาแบบทวิภาค (ต้องเรียงลำดับก่อน)
  • สำหรับ LINQ ใช้ array.Any(predicate) แทนได้ — อ่านง่ายกว่า

ตัวอย่างเพิ่มเติม: Array static methods

int[] nums = { 5, 2, 8, 1, 9, 3 };

bool anyGt5 = Array.Exists(nums, n => n > 5);           // true
bool allGt0 = Array.TrueForAll(nums, n => n > 0);       // true
int firstGt5 = Array.Find(nums, n => n > 5);            // 8
int[] allGt5 = Array.FindAll(nums, n => n > 5);         // {8, 9}
int idx = Array.FindIndex(nums, n => n > 5);            // 2
Array.Sort(nums);                                       // {1,2,3,5,8,9}
int pos = Array.BinarySearch(nums, 5);                  // 3