Last active
May 25, 2017 18:35
-
-
Save jbubriski/d2bd680a58089896de64e7e0266b1bb3 to your computer and use it in GitHub Desktop.
C# SubDomain Parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WIMMY WAM WAM WOZZLE!!!