Solved

String Parsing in Vb.Net or C#.Net

Posted on 2014-10-03
8
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40359515
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
ID: 40359571
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 75

Expert Comment

by:käµfm³d 👽
ID: 40359575
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:chokka
ID: 40359586
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
 
LVL 75

Expert Comment

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

Author Comment

by:chokka
ID: 40359610
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 75

Expert Comment

by:käµfm³d 👽
ID: 40359624
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
ID: 40359666
Thanks !!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

729 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