Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

String Parsing in Vb.Net or C#.Net

Posted on 2014-10-03
8
Medium Priority
?
309 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 2000 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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

Industry Leaders: 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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

721 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