[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

String Parsing in Vb.Net or C#.Net

I have a uri string,

Sample format :- ftp://hostname:portnumber/../../folderpath/

Expected format would be :- ftp://hostname:portnumber/../../myfolder/myfolder/myfolder/

hostname could be an Ip config or computername

I have to parse the uri string  and pick the HostName, PortNumber and Folder Path seperately.

On what logic, I have to go through this process.
0
chokka
Asked:
chokka
  • 4
  • 4
1 Solution
 
käµfm³d 👽Commented:
Use the Uri class:

e.g.

Uri u = new Uri("ftp://hostname:portnumber/../../folderpath/");
string host = u.Host;
int port = u.Port;
string path = u.PathAndQuery;

Open in new window

0
 
chokkaAuthor Commented:
Great, I was worried that i should go for some substring function.

I didnt know that Uri is an inbuilt class.

Thank you !!!
0
 
käµfm³d 👽Commented:
The only caveat I would provide is that if you for some reason need the "/../.." part, then you may need to do string parsing--the Uri class seems to consume that bit; not sure why.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
chokkaAuthor Commented:
Yes, i am facing that issue.


 Dim sPath As String = String.Empty

        sPath = sUri.PathAndQuery.ToString()

        sPath = sPath.Replace("%2F", "/")
        sPath = sPath.Replace("%2E", "/")

sPath = sPath.Replace("/../../","")

Open in new window

0
 
käµfm³d 👽Commented:
OK, for clarity:  Do you need "../../myfolder/myfolder/myfolder/" or "myfolder/myfolder/myfolder/" returned?
0
 
chokkaAuthor Commented:
I need "myfolder/myfolder/myfolder/"


My requirement is to get the uri from config file and create a sftp link.

sftp://username:password@hostname:portnumber//folderpath/

If i am able to get "myfolder/myfolder/myfolder/" , I can concatenate with the existing string
0
 
käµfm³d 👽Commented:
As far as I saw, that is what the code I posted above returns:

Screenshot
All I was saying earlier was that if for some reason it needed to be:

"../../folderpath/"

...that you saw in the path variable, then the Uri class seems to mangle that bit, as visible in the above image.
0
 
chokkaAuthor Commented:
Thanks !!
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now