Click here to Skip to main content
15,891,006 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
FTPAddress is coming from foreach loop.

FTPAddress="ftp://me@localhost/newfolder/newfolder1/"

myFTP string = FTPAddress+file

i want to save it in local drive as

newfolder/newfolder1/file


My question is how do i split the FTPAddress string?
Posted
Comments
Aarti Meswania 16-Apr-13 4:00am    
what should be final ftp string?
Friendsaa 16-Apr-13 4:58am    
I want just path not ftp string, path from which folder its coming?

1 solution

Use a regex:
C#
private static Regex regex = new Regex(
      "^(?<Host>.*@.*?)/(?<Path>.*)$",
    RegexOptions.IgnoreCase
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );

This spilts your string into two groups: Host and Path. - you want the Path only.


"thank you OriginalGriff, But how to use it :("


C#
string s = "ftp://me@localhost/newfolder/newfolder1/";
    Match m = splitFTP.Match(s);
    if (m.Success)
        {
        Console.WriteLine(m.Groups["Host"].Value);
        Console.WriteLine(m.Groups["Path"].Value);
        }
    ...

private static Regex splitFTP = new Regex("^(?<Host>.*@.*?)/(?<Path>.*)$",
                                          RegexOptions.IgnoreCase
                                          | RegexOptions.CultureInvariant
                                          | RegexOptions.IgnorePatternWhitespace
                                          | RegexOptions.Compiled);
Will print:
ftp://me@localhost
newfolder/newfolder1/
 
Share this answer
 
v2
Comments
Friendsaa 16-Apr-13 4:57am    
thank you OriginalGriff, But how to use it :(
OriginalGriff 16-Apr-13 5:13am    
Answer updated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900