C# - Split a path into a string[]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deridder149
    New Member
    • Apr 2007
    • 1

    C# - Split a path into a string[]

    Hello,

    I am working on a quick file browser project. i need to find a way to split a path:

    (c:/somedir/somefile.txt)

    into a string array:

    [0] c
    [1] somedir
    [2] somefile.txt

    is there any built in functionality to do this in c#? if not, does anybody know of any way that i can do this? Thank you very much for any help that you might be able to give, it will be much appreciated.

    thanks, Andrew.
  • prabunewindia
    New Member
    • Mar 2007
    • 199

    #2
    hi friend,
    plz try with this one

    string a = "c:/somedir/somefile.txt";
    string[] arr = a.Split('/');
    and now u can get the separate strings as u like

    prabu

    I am working on a quick file browser project. i need to find a way to split a path:

    (c:/somedir/somefile.txt)

    into a string array:

    [0] c
    [1] somedir
    [2] somefile.txt

    is there any built in functionality to do this in c#? if not, does anybody know of any way that i can do this? Thank you very much for any help that you might be able to give, it will be much appreciated.

    thanks, Andrew.[/QUOTE]

    Comment

    • predsedateli
      New Member
      • Mar 2007
      • 3

      #3
      hi!!
      try this method:
      add:
      Code:
      using System.Text.RegularExpressions;
      
      string inputString = @"C:\somedir\somefile.txt";
      string[] parts = Regex.Split(inputString,@"\\");
      			foreach (string s in parts)
      			{
                                          // process the strings
                 }
      :)

      Originally posted by deridder149
      Hello,

      I am working on a quick file browser project. i need to find a way to split a path:

      (c:/somedir/somefile.txt)

      into a string array:

      [0] c
      [1] somedir
      [2] somefile.txt

      is there any built in functionality to do this in c#? if not, does anybody know of any way that i can do this? Thank you very much for any help that you might be able to give, it will be much appreciated.

      thanks, Andrew.

      Comment

      Working...