检查字符串中的 URL 的 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-Jun-2020

2K+ 浏览次数

开启你的 职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.