I'm still relatively new to C# programming and I'm having trouble parsing a URL string. I would like to take a URL like http://server.example.org and split it into separate strings like protocol, server, and domain name. Here is what my code looks like so far:
string url = "http://server.example.org";
string protocol = url.Substring(0, url.IndexOf(":")); // parse from beginning to colon to get protocol
string server = url.Substring(url.IndexOf("//") + 2, url.IndexOf(".")); // parse from // + 2 to first . for server
string domain = url.Substring(url.IndexOf(".") + 1); // the rest of the string should be the domain
The protocol string correctly shows http and domain correctly shows example.org, but the server string comes out as server.exampl instead of server as expected. What am I doing wrong?