Skip to content

Instantly share code, notes, and snippets.

@jbubriski
Last active May 25, 2017 18:35
Show Gist options
  • Save jbubriski/d2bd680a58089896de64e7e0266b1bb3 to your computer and use it in GitHub Desktop.
Save jbubriski/d2bd680a58089896de64e7e0266b1bb3 to your computer and use it in GitHub Desktop.
C# SubDomain Parsing
void Main()
{
var testCases = new[] {
null,
"",
"localhost",
"johnnycode.com",
"blog.johnnycode.com",
"a.b.c.blog.johnnycode.com"
};
foreach (var testCase in testCases)
{
var subDomains = GetSubDomains(testCase);
Console.WriteLine("Test Case: " + testCase);
if (subDomains != null && subDomains.Length > 0)
Console.WriteLine("Results: " + string.Join(",", subDomains));
else
Console.WriteLine("None");
Console.WriteLine();
}
}
public string[] GetSubDomains(string host)
{
if (string.IsNullOrWhiteSpace(host))
return null;
var parts = host.Split('.');
if (parts.Length > 2)
return parts.Take(parts.Length - 2).ToArray();
else if (parts.Length == 2)
return new string[0];
else if (parts.Length == 1)
return new string[0];
return parts;
}
@jbubriski
Copy link
Author

WIMMY WAM WAM WOZZLE!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment