Results 1 to 4 of 4

Thread: Split Function <RESOLVED>

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Malaysia
    Posts
    345

    Split Function <RESOLVED>

    Hi!

    I am develeoping an application in asp.net with c# as the code behind.
    I want to extract the file name form a string say, "C\documents\abc\abc.doc".

    I have to get abc.doc in a variable.
    I wanted to use the Split function and get the last item of the array, which would have given me the file name.

    But, I am not been able to do so.

    My code is

    Code:
    string strDocumentName = System.Web.HttpContext.Current.Request.Files[0].FileName.ToString();
    string[]arrFile = strDocumentName.Split('\');
    
    //Loop through the array arrFile
    for(int i=0;i<=arrFile.GetUpperBound(0);i++)
    {
       strDocumentName = arrFile.GetValue(i).ToString();
    }
    Th problem is Split function gives an error with the delimeter (\). With every other delimiter it works fine.

    Pls help.
    Last edited by sinha; Aug 31st, 2005 at 03:46 PM.
    Thanks.

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Split Function

    Check out the IO.PATH name space. In VB.Net you would use:
    VB Code:
    1. dim sFilename as String = system.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.Files[0].FileName)

  3. #3
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: Split Function

    \ is an escape character - to fix your method, use Split('\\') instead.

    wild_bill's option is somewhat cleaner though

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Malaysia
    Posts
    345

    Re: Split Function

    Thanks a bunch
    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width