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. split string - better way?

split string - better way?

Scheduled Pinned Locked Moved C / C++ / MFC
question
14 Posts 8 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.
  • T Tara14

    Hi, I have to split a string in the format mm/dd/yyyy. This is what I do:

    CString date,sMM,sDD,sYY,str;
    int	yy,mm,dd; 
    
    // date\_poleSet = CString object date in the format mm/dd/yyyy
    // the month,day and year have to be seperated into int mm,dd,yy
    
    int offset = date\_poleSet.Find('/');
    date = date\_poleSet;
    sMM = date\_poleSet.Left(offset); 
    mm = atoi(sMM); // gives the month
    str = date.Mid(offset+1);
    
    offset = str.Find('/');
    date = str;
    sDD = str.Left(offset);
    dd = atoi(sDD); // gives the day
    sYY = date.Mid(offset+1);
    yy = atoi(sYY); // gives the year
    

    Is this good enough? Or can it be done in two or three lines?

    Thanks, Tara

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #3

    Have you considered COleDateTime::ParseDateTime()?

    "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

    C 1 Reply Last reply
    0
    • C CPallini

      if your mm/dd/yyyy hypothesis holds then

      mm = atoi(str.Left(2));
      dd = atoi(str.Mid(3,2));
      yy = atoi(str.Right(2));

      is a bit more concise.

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      T Offline
      T Offline
      Tara14
      wrote on last edited by
      #4

      Wow! Thats nice. Thanks a billion.

      Thanks, Tara

      1 Reply Last reply
      0
      • T Tara14

        Hi, I have to split a string in the format mm/dd/yyyy. This is what I do:

        CString date,sMM,sDD,sYY,str;
        int	yy,mm,dd; 
        
        // date\_poleSet = CString object date in the format mm/dd/yyyy
        // the month,day and year have to be seperated into int mm,dd,yy
        
        int offset = date\_poleSet.Find('/');
        date = date\_poleSet;
        sMM = date\_poleSet.Left(offset); 
        mm = atoi(sMM); // gives the month
        str = date.Mid(offset+1);
        
        offset = str.Find('/');
        date = str;
        sDD = str.Left(offset);
        dd = atoi(sDD); // gives the day
        sYY = date.Mid(offset+1);
        yy = atoi(sYY); // gives the year
        

        Is this good enough? Or can it be done in two or three lines?

        Thanks, Tara

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #5

        You could also use a COleDateTime[^] object: you "load" the date using the ParseDateTime method and then you can retrieve all the information you need using the different member functions. The big advantage is that it checks for the date validity, which is a pain if you have to do it yourself (months don't have the same number of days and you have to take into account leap years too :sigh: ).

        Cédric Moonen Software developer
        Charting control [v1.3]

        T 1 Reply Last reply
        0
        • D David Crow

          Have you considered COleDateTime::ParseDateTime()?

          "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #6

          Damn, you beat me to it ;P

          Cédric Moonen Software developer
          Charting control [v1.3]

          D T 2 Replies Last reply
          0
          • C Cedric Moonen

            Damn, you beat me to it ;P

            Cédric Moonen Software developer
            Charting control [v1.3]

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #7

            I guess my CPMRU was fully charged.

            "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            R C 2 Replies Last reply
            0
            • C Cedric Moonen

              You could also use a COleDateTime[^] object: you "load" the date using the ParseDateTime method and then you can retrieve all the information you need using the different member functions. The big advantage is that it checks for the date validity, which is a pain if you have to do it yourself (months don't have the same number of days and you have to take into account leap years too :sigh: ).

              Cédric Moonen Software developer
              Charting control [v1.3]

              T Offline
              T Offline
              Tara14
              wrote on last edited by
              #8

              Thank you. I shall look into ParseDateTime. Didn't know about that! Although for now, what you showed me (left,right, mid) is perfect for what I am trying to do.

              Thanks, Tara

              C 1 Reply Last reply
              0
              • T Tara14

                Thank you. I shall look into ParseDateTime. Didn't know about that! Although for now, what you showed me (left,right, mid) is perfect for what I am trying to do.

                Thanks, Tara

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #9

                Tara14 wrote:

                what you showed me

                It was not me ;) Anyway, it all depends from where your date is coming from: if it entered by a user of your program, then it is better to check its validity.

                Cédric Moonen Software developer
                Charting control [v1.3]

                1 Reply Last reply
                0
                • D David Crow

                  I guess my CPMRU was fully charged.

                  "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  R Offline
                  R Offline
                  Rajesh R Subramanian
                  wrote on last edited by
                  #10

                  Keep it in mind that no matter how charged it is, there may be some queries with which the CPMRU will not help you at all. ;P

                  Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                  M 1 Reply Last reply
                  0
                  • R Rajesh R Subramanian

                    Keep it in mind that no matter how charged it is, there may be some queries with which the CPMRU will not help you at all. ;P

                    Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #11

                    Indeed. That's a documented bug....won't be fixed for a few releases. Good luck.

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    1 Reply Last reply
                    0
                    • D David Crow

                      I guess my CPMRU was fully charged.

                      "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                      C Offline
                      C Offline
                      crossie
                      wrote on last edited by
                      #12

                      Can I ask what CPMRU is short for?

                      C 1 Reply Last reply
                      0
                      • C Cedric Moonen

                        Damn, you beat me to it ;P

                        Cédric Moonen Software developer
                        Charting control [v1.3]

                        T Offline
                        T Offline
                        ThatsAlok
                        wrote on last edited by
                        #13

                        Cedric Moonen wrote:

                        you beat me to it

                        Using Sticks?? :doh: :laugh:

                        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                        Never mind - my own stupidity is the source of every "problem" - Mixture

                        cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>

                        1 Reply Last reply
                        0
                        • C crossie

                          Can I ask what CPMRU is short for?

                          C Offline
                          C Offline
                          Cedric Moonen
                          wrote on last edited by
                          #14

                          It is a 'private' joke. It means "Code Project Mind Reader Unit". It appeared recently because a lot of questions that were asked were so incomplete that you had to either see the screen of the guy or read his mind :rolleyes:

                          Cédric Moonen Software developer
                          Charting control [v1.3]

                          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