0

Is there a way to get the main site text on a string.

Strings

string site1 = "https://www.google.com/xxxxxxxx";

string site2 = "https://www.youtube.com/xxxxxxxxx";

Getting Result should be

string getsite1 = "https://www.google.com";

string getsite2 = "https://www.youtube.com";

Neppy
  • 13
  • 2
  • 7
    The [Uri](https://learn.microsoft.com/en-us/dotnet/api/system.uri?view=netframework-4.8) class might do what you need – npinti Jul 17 '19 at 08:15
  • 3
    Welcome to stackoverflow. Please take a minute to take the [tour], especially How to [Ask], and [edit] your question accordingly. – Zohar Peled Jul 17 '19 at 08:18

3 Answers3

2

You can get the authority part of the URL which is the scheme and the host like this

    string site1 = "https://www.google.com/xxxxxxxx";
    string site2 = "https://www.youtube.com/xxxxxxxxx";

    string getsite1 = new Uri(site1).GetLeftPart(UriPartial.Authority);
    string getsite2 = new Uri(site2).GetLeftPart(UriPartial.Authority);

    Console.WriteLine(getsite1);
    Console.WriteLine(getsite2);

this prints

https://www.google.com
https://www.youtube.com
Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
0

You can use the System.Uri class.

string site1 = "https://www.google.com/xxxxxxxx";
var uri = new Uri(site1);
var getsite1 = uri.Scheme + "://" + uri.Host;
Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67
0

Like npinti wrote in the comment you need the Uri Class. Then you can create something like this:

string site1 = "https://www.google.com/xxxxxxxx";

var uri = new Uri(site1);

var result = uri.GetLeftPart(UriPartial.Authority);
Presi
  • 806
  • 10
  • 26