C# 程序检查字符串中的 URL

csharpserver side programmingprogramming更新于 2024/9/24 18:00:00

使用 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";
      // 查看输入是否与其中一个开头匹配。
      if (input.StartsWith("https://www.example.com") || input.StartsWith("https://example.com")) {
         Console.WriteLine(true);
      }
   }
}

输出

True

相关文章