Home  >  Article  >  Backend Development  >  C# program to check URL in string

C# program to check URL in string

王林
王林forward
2023-08-28 13:21:11868browse

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

Use the StartWith() method in C# to check for URL in a String.

Let us say our input string is −

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

Now we need to check www or non-www links. To do this, use an if statement in C# −

if (input.StartsWith("https://www.example.com") || input.StartsWith("https://example.com")) {
}

Example

You can try to run the following code to check for URL in a string.

Live Demo

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);
      }
   }
}

Output

True

The above is the detailed content of C# program to check URL in string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete