split()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • veerapureddy
    New Member
    • Jun 2007
    • 21

    split()

    Hi friends,

    i need some urgent help from u.

    i would like to divide a string using a "\"(pathseparat or) as delimeter with Split method.

    String str="c:\documen ts\csr\text.txt

    i need the last word text.txt.

    String str1 =str.split('\')[];

    i used like this ,but i am not getting the output,please help me.i tried it in google and in some other forums, but i didnt get.

    and another one, is there any way to get last word directly without looping the array?

    please as early as possible

    Thanks in advance
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You would need to use:
    Code:
    //Note: The @ just means you don't want to escape any characters like \
    string str=@"c:\documents\csr\text.txt";
    string[] pieces = str.Split("/".ToCharArray());
    string myending=pieces[pieces.Length - 1];
    \\myending should now contain "text.txt"
    Although, if you are only dealing with paths/files. Using System.IO.File and System.IO.FileI nfo (and counter parts for Directory/DirectoryInfo) might also be used to do what you want.

    Comment

    • nikpreek
      New Member
      • Jun 2007
      • 22

      #3
      You can use one of these: (Code is in C#)
      1.
      [CODE=c]
      sResult = @"C:\Temp\myfil e.txt";
      int index = sResult.LastInd exOf('\\');
      sResult = sResult.Substri ng(index + 1);
      [/CODE]
      2. Or, if you are using this for file or dir path, you can import System.IO namespace. Then u can use static functions from Path or File or Directory classes to break your path into directory and file name (with or without extn etc.)
      Paste this code and call it from a windows form
      [CODE=c]
      public string PlayPathFleName Etc(string sPath)
      {
      textBox1.Text = "*** GetDirectoryNam e: " + Path.GetDirecto ryName(sPath);
      textBox1.Text += Environment.New Line + "*** GetExtension: " + Path.GetExtensi on(sPath);
      textBox1.Text += Environment.New Line + "*** GetFileName: " + Path.GetFileNam e(sPath);
      textBox1.Text += Environment.New Line + "*** GetFileNameWith outExtension: " + Path.GetFileNam eWithoutExtensi on(sPath);
      textBox1.Text += Environment.New Line + "*** GetFullPath: " + Path.GetFullPat h(sPath);
      textBox1.Text += Environment.New Line + "*** GetPathRoot: " + Path.GetPathRoo t(sPath);
      textBox1.Text += Environment.New Line + "*** GetRandomFileNa me: " + Path.GetRandomF ileName();
      textBox1.Text += Environment.New Line + "*** GetTempFileName : " + Path.GetTempFil eName();
      textBox1.Text += Environment.New Line + "*** GetTempPath: " + Path.GetTempPat h();

      return "Success, check TextBox Contents.";
      }
      [/CODE]
      Last edited by nikpreek; Jun 19 '07, 01:40 PM. Reason: Updated Code Tag

      Comment

      Working...