CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Cutting String

  1. #1
    Join Date
    Dec 2008
    Posts
    6

    Cutting String

    using List<string> to store them.
    There is a list of strings as shown below:

    "C:\WithCreationFilter_CS\bin\Debug\hello.dll"
    "C:\AAA\bin\Debug\XXX.dll"
    "C:\ZZZ\bin\Debug\YYY.dll"
    "C:\QQQ\XXX\YYY\bin\Debug\xxx.dll"

    How to get expected output as shown below??

    Expected ouput:

    "hello.dll"
    "XXX.dll"
    "YYY.dll"
    "xxx.dll"

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Cutting String

    Code:
    string s = "C:\\WithCreationFilter_CS\\bin\\Debug\\hello.dll";
    
    string result = s.Remove(s.LastIndexOf("\\"));

  3. #3
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Cutting String

    Quote Originally Posted by dannystommen View Post
    Code:
    string s = "C:\\WithCreationFilter_CS\\bin\\Debug\\hello.dll";
    
    string result = s.Remove(s.LastIndexOf("\\"));
    I think you meant SubString instead of Remove. Your code returns "C:\WithCreationFilter_CS\bin\Debug"

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Cutting String

    All he needs is the file name to be returned from the full path. How about trying Path.GetFileName() method? Something like this
    Code:
    string FileName = Path.GetFileName(@"C:\WithCreationFilter_CS\bin\Debug\hello.dll" );

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Cutting String

    Quote Originally Posted by Shuja Ali View Post
    All he needs is the file name to be returned from the full path. How about trying Path.GetFileName() method? Something like this
    Code:
    string FileName = Path.GetFileName(@"C:\WithCreationFilter_CS\bin\Debug\hello.dll" );
    This is the way to go as it also checks for invalid characters and other edge cases.

  6. #6
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Cutting String

    So complete sample could look like:
    Code:
    var paths = new List<string>
    {
     "C:\WithCreationFilter_CS\bin\Debug\hello.dll",
     "C:\AAA\bin\Debug\XXX.dll" ,
     "C:\ZZZ\bin\Debug\YYY.dll" ,
     "C:\QQQ\XXX\YYY\bin\Debug\xxx.dll" 
    }
    
    var fileNames = paths.Select(p => Path.GetFileName(p)).ToList();
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  7. #7
    Join Date
    Feb 2009
    Location
    Atlanta, GA
    Posts
    17

    Re: Cutting String

    It seems I answered this question yesterday on another forum. Don't you love cross posters?
    Scott Knake
    Custom Software Development
    Apex Software, Inc.

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

Featured