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

วิธีหา Substring จาก String ใน C#

กำหนดสตริง:

string s = "Tom Cruise";

ใช้ Contains() หา "Tom":

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

ตัวอย่าง

using System;
public class Demo {
    public static void Main() {
        string s = "Tom Cruise";
        if (s.Contains("Tom") == true) {
            Console.WriteLine("Substring found!");
        } else {
            Console.WriteLine("Substring not found!");
        }
    }
}

ผลลัพธ์

Substring found!