Solved

String Parsing in Vb.Net or C#.Net

Posted on 2014-10-03
8
287 Views
Last Modified: 2014-10-03
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
Comment
Question by:chokka
  • 4
  • 4
8 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:chokka
Comment Utility
Great, I was worried that i should go for some substring function.

I didnt know that Uri is an inbuilt class.

Thank you !!!
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
 

Author Comment

by:chokka
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
OK, for clarity:  Do you need "../../myfolder/myfolder/myfolder/" or "myfolder/myfolder/myfolder/" returned?
0
 

Author Comment

by:chokka
Comment Utility
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
 

Author Comment

by:chokka
Comment Utility
Thanks !!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now