CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2013
    Posts
    21

    CString replace? index of?

    Is there a function that can take out part of the string such as,


    C:/Users

    I want to take out Users.

    I need the function to search for the first "/" and take out the last dir name

    C:/windows would be c:
    C:/users/bla would be c/users

    Im programming in visual studio 6.0 MFC

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: CString replace? index of?

    Quote Originally Posted by vaas View Post
    Is there a function that can take out part of the string such as,


    C:/Users

    I want to take out Users.

    I need the function to search for the first "/" and take out the last dir name

    C:/windows would be c:
    C:/users/bla would be c/users

    Im programming in visual studio 6.0 MFC
    From the examples you posted, it looks more like you want to find the last /, in which case, ReverseFind and Left would give you what you need.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: CString replace? index of?

    Quote Originally Posted by vaas View Post
    Is there a function that can take out part of the string such as,
    If you're working directly with path and file names, you should use what the CRT offers, and that is _splitpath.

    http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: CString replace? index of?

    _splitpath doesn't split up subdirectories which the OP is asking about...

    What you can do can be achieved by:
    Use CString::ReverseFind() to find the last \ (you may or may not have to account for the fact the path may not have a \ or may be ending on a \)

    Use CString::Mid() or CString::Right() or CString::Left() to extract the part you want

  5. #5
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: CString replace? index of?

    Building on what Paul said, since you are on Windows - I would use Shell Path Handling Functions.
    Other than for exercise, it is a bad idea to work with the file names as strings.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

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