-3

User input the url string in a textbox and I need to add a string "cmd" if not available .

Please suggest how to achieve this,

string cmdUrl = AddPrefix("https://google.com");

static string AddPrefix(string inputUrl)
{
   return formattedUrl;// want to return https://cmd.google.com if the string cmd not added already in url
}
Sara
  • 55
  • 5
  • There's functionality for parsing a URL/URI into its component parts and then manipulating it. Did you do any research for built in methods to do that in .NET? – mason May 05 '22 at 13:15
  • Does this answer your question? [URL split in C#?](https://stackoverflow.com/questions/1029909/url-split-in-c) – rekcul May 05 '22 at 13:29

1 Answers1

0

First, you can parse your url using var parsedUrl = new Uri(inputUrl). Then check if the host includes your substring like this: parsedUrl.Host.StartsWith("cmd") If it does, return your original url else build your new one: $"{parsedUrl.Scheme}://cmd.{parsedUrl.Host}{parsedUrl.PathAndQuery}"

Lukas Hein
  • 65
  • 1
  • 5