ใช้เมธอด StartWith() ใน C# เพื่อตรวจสอบ URL ในสตริง
สมมติว่าสตริงอินพุตของเราคือ -
string input = "https://example.com/new.html";
ตอนนี้เราต้องตรวจสอบลิงก์ www หรือไม่มี www สำหรับสิ่งนี้ ให้ใช้คำสั่ง if ใน C# −
if (input.StartsWith("https://www.example.com") || input.StartsWith("https://example.com")) {
} ตัวอย่าง
คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อตรวจสอบ URL ในสตริง
using System;
class Demo {
static void Main() {
string input = "https://example.com/new.html";
// See if input matches one of these starts.
if (input.StartsWith("https://www.example.com") || input.StartsWith("https://example.com")) {
Console.WriteLine(true);
}
}
} ผลลัพธ์
True