Sử dụng phương thức Contains () để kiểm tra xem một chuỗi có chứa một từ hay không.
Đặt chuỗi -
string s = "Together we can do so much!";
Bây giờ, giả sử bạn cần tìm từ “nhiều”
if (s.Contains("much") == true) {
Console.WriteLine("Word found!");
} Hãy cho chúng tôi xem mã hoàn chỉnh -
Ví dụ
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!");
}
}
} Đầu ra
Word found!