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

วิธีตรวจสอบว่าสตริงมีคำเฉพาะหรือไม่ใน C#

ใช้ Contains() ตรวจสอบคำในสตริง

string s = "Together we can do so much!";

ค้นหาคำ "much":

if (s.Contains("much") == true) {
    Console.WriteLine("Word found!");
}

ตัวอย่าง

using System;
public class Demo {
    public static void Main() {
        string s = "Together we can do so much!";
        if (s.Contains("much") == true) {
            Console.WriteLine("Word found!");
        } else {
            Console.WriteLine("Word not found!");
        }
    }
}

ผลลัพธ์

Word found!