KING
KING

Reputation: 980

URI String Manipulation in C#

My question is simple, I need to turn this

"devlxxx/mxxxxxxxxx/xxx.asmx" into this "devlxxxmxxxxxxxxxxxx".

The catch is that, In my application a user submits a webservice, I save the URI as a string and take its

    uri.Host + uri.PathAndQuery 

and set that as a new string. This leaves me with "devlxxx/mxxxxxxxxx/xxx.asmx". The Web service URI can be any length in size so the length is not Constant. My first theoretical approach was to remove the slashes by interating over the string and removing them whereever they occured. Regardless the final out put should return the first part of the URI (before the first forward slash) and the second part of the url (text between the first forward slash and the second), and removing the rest of the text..... Any theories of code snippits would be much appreciated.

Upvotes: 0

Views: 724

Answers (1)

Habib
Habib

Reputation: 223392

One way can be to remove / and then use Path.GetFileNameWithoutExtension.

string str = "devlxxx/mxxxxxxxxx/xxx.asmx";
str = str.Replace("/", "");
string requiredStr = Path.GetFileNameWithoutExtension(str);

and you will get: requiredStr = "devlxxxmxxxxxxxxxxxx"

Although its not exactly a file name but this trick should do the work.

Upvotes: 2

Related Questions