-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 } 
2
  • 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? Commented May 5, 2022 at 13:15
  • Does this answer your question? URL split in C#? Commented May 5, 2022 at 13:29

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.