Solved

String Parsing in Vb.Net or C#.Net

Posted on 2014-10-03
8
305 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
Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

630 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