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. CString Mid()

CString Mid()

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 4 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
    RadioOpa
    wrote on last edited by
    #1

    I have a CString object loaded with a string like "This is the first section$This is the second section$This is the end" so each section separated by '$'. I try to load the second section as csAnsprech = csComboBoxText.Mid( csComboBoxText.Find( '$') +1, csComboBoxText.ReverseFind( '$')); but this retrieves the second and third section, nut just the second one. What´s wrong here ?

    N L M 3 Replies Last reply
    0
    • R RadioOpa

      I have a CString object loaded with a string like "This is the first section$This is the second section$This is the end" so each section separated by '$'. I try to load the second section as csAnsprech = csComboBoxText.Mid( csComboBoxText.Find( '$') +1, csComboBoxText.ReverseFind( '$')); but this retrieves the second and third section, nut just the second one. What´s wrong here ?

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      int nPos = csComboBoxText.Find( '$') + 1; csAnsprech = csComboBoxText.Mid( nPos , csComboBoxText.GetLength()- csComboBoxText.Find( '$',nPos )); nave

      1 Reply Last reply
      0
      • R RadioOpa

        I have a CString object loaded with a string like "This is the first section$This is the second section$This is the end" so each section separated by '$'. I try to load the second section as csAnsprech = csComboBoxText.Mid( csComboBoxText.Find( '$') +1, csComboBoxText.ReverseFind( '$')); but this retrieves the second and third section, nut just the second one. What´s wrong here ?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Hello RadioOpa, try this: int i =0; while(i < csComboBoxText.GetLength()) { if(csComboBoxText.GetAt(i) == '$') // if we found the first separator in the string { i++; // move one place to skip the separator while(csComboBoxText.GetAt(i) != '$') // now copy all characters in your string, while not found the next separator { csAnsprech += csComboBoxText.GetAt(i); i++; // move to the next character } } i++;// move to the next character } i hope that helps you! regards break; P.S. that sample from nave ist very good, i want just to show you another way without Find-Functions to extract a part of string -- modified at 2:55 Tuesday 21st March, 2006

        R 1 Reply Last reply
        0
        • L Lost User

          Hello RadioOpa, try this: int i =0; while(i < csComboBoxText.GetLength()) { if(csComboBoxText.GetAt(i) == '$') // if we found the first separator in the string { i++; // move one place to skip the separator while(csComboBoxText.GetAt(i) != '$') // now copy all characters in your string, while not found the next separator { csAnsprech += csComboBoxText.GetAt(i); i++; // move to the next character } } i++;// move to the next character } i hope that helps you! regards break; P.S. that sample from nave ist very good, i want just to show you another way without Find-Functions to extract a part of string -- modified at 2:55 Tuesday 21st March, 2006

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

          Many thanks, both ways solving the problem are excellent. I changed code as follows: iFirstSection = csComboBoxText.Find( '$'); // Find first '$' iSecondSection = csComboBoxText.Find( '$', iFirstSection +1); // Find second '$' csFirma = csComboBoxText.Mid( 0, iFirstSection); // Get first string section csFirma.TrimRight( ' '); csOrt = csComboBoxText.Mid( iFirstSection +1, iSecondSection - iFirstSection); // Get second csOrt.TrimRight( ' '); csFirmaID = csComboBoxText.Mid( iSecondSection +1); // Get last section

          1 Reply Last reply
          0
          • R RadioOpa

            I have a CString object loaded with a string like "This is the first section$This is the second section$This is the end" so each section separated by '$'. I try to load the second section as csAnsprech = csComboBoxText.Mid( csComboBoxText.Find( '$') +1, csComboBoxText.ReverseFind( '$')); but this retrieves the second and third section, nut just the second one. What´s wrong here ?

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            The second param to Mid() is the # of characters, not the ending index. You can use AfxExtractSubString() to pick out the tokens. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

            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