Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add prefix string in URL string

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
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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}"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading