使用 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.