Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Splitting a CString

Splitting a CString

Scheduled Pinned Locked Moved C / C++ / MFC
perltutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ROK_RShadow
    wrote on last edited by
    #1

    Is there a way to split a CString as you can in Perl? Need to find a way to take a string and split it based on a delemiter. for example if I have a string that looks like c:\data\moredata\evenmoredata\datafile.dat I need to be able to split "datafile" from the string. Since the open file Dialog supports multiple selections I can't use .GetFileTitle(); Any ideas?

    P B 2 Replies Last reply
    0
    • R ROK_RShadow

      Is there a way to split a CString as you can in Perl? Need to find a way to take a string and split it based on a delemiter. for example if I have a string that looks like c:\data\moredata\evenmoredata\datafile.dat I need to be able to split "datafile" from the string. Since the open file Dialog supports multiple selections I can't use .GetFileTitle(); Any ideas?

      P Offline
      P Offline
      Pavel Klocek
      wrote on last edited by
      #2

      I use this[^]. Pavel Sonork 100.15206

      1 Reply Last reply
      0
      • R ROK_RShadow

        Is there a way to split a CString as you can in Perl? Need to find a way to take a string and split it based on a delemiter. for example if I have a string that looks like c:\data\moredata\evenmoredata\datafile.dat I need to be able to split "datafile" from the string. Since the open file Dialog supports multiple selections I can't use .GetFileTitle(); Any ideas?

        B Offline
        B Offline
        Big Art
        wrote on last edited by
        #3

        Hello there, Use CString's ReverseFind(..) function to get the position of the first backslash starting the search from the end of the string then use the Right(..) function to extract the datafile.dat name. If you don't want the extenstion you could use the Reverse find funcion again on the initial string and use the Mid(..) function to extract the name between the last backslash and the period. Alternatively you could have called CString's GetBuffer(..) on the string and tokenize the result it using strtok() or some other low lever function. I would advise the first approach. Art

        R 1 Reply Last reply
        0
        • B Big Art

          Hello there, Use CString's ReverseFind(..) function to get the position of the first backslash starting the search from the end of the string then use the Right(..) function to extract the datafile.dat name. If you don't want the extenstion you could use the Reverse find funcion again on the initial string and use the Mid(..) function to extract the name between the last backslash and the period. Alternatively you could have called CString's GetBuffer(..) on the string and tokenize the result it using strtok() or some other low lever function. I would advise the first approach. Art

          R Offline
          R Offline
          ROK_RShadow
          wrote on last edited by
          #4

          I Looked over MSDN information on both ReverseFind() and strtok() before you posted this information. I did not really understand the ReverseFind() function when I first read the information, and ended up implementing strtok() to do the work, however the code that I am using to do it looks rather nasty.. but it works.. however I will look more into ReverseFind() and see if I can implement this for hopefully cleaner code. Thank you very much for the information.

          B 1 Reply Last reply
          0
          • R ROK_RShadow

            I Looked over MSDN information on both ReverseFind() and strtok() before you posted this information. I did not really understand the ReverseFind() function when I first read the information, and ended up implementing strtok() to do the work, however the code that I am using to do it looks rather nasty.. but it works.. however I will look more into ReverseFind() and see if I can implement this for hopefully cleaner code. Thank you very much for the information.

            B Offline
            B Offline
            Big Art
            wrote on last edited by
            #5

            Here you are: CString sPath(_T("c:\\data\\moredata\\evenmoredata\\datafile.dat")); int nPos1 = sPath.ReverseFind('\\'); int nPos2 = sPath.ReverseFind('.'); if(nPos1 > -1 && nPos2 > -1) { CString sFile = sPath.Mid(nPos1+1, nPos2 - nPos1 -1); AfxMessageBox(sFile); } ----------------- The ReverseFind fn just searches the string starting from the end toward the beginning and stops when it encounters the character it is looking for. The position it reports is referenced from the beginning of the string. Art

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups