使用 C# 程式檢查字串中的網址


在 C# 中使用 StartWith() 方法檢查字串中的 URL。

假設我們的輸入字串為 -

string input = "https://example.com/new.html";

現在,我們需要檢查帶有 www 或不帶 www 的連結。為此,在 C# 中使用 if 語句 -

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

更新於: 19-6-2020

2 千次以上瀏覽

開啟你的 職業生涯

完成課程以獲得認證

開始學習
廣告
© . All rights reserved.