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. String Comparing

String Comparing

Scheduled Pinned Locked Moved C / C++ / MFC
xml
8 Posts 5 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.
  • B Offline
    B Offline
    brucewayn
    wrote on last edited by
    #1

    I am working for an application where i am supposed to display the latest data on a list box from a XML. I got the datetime node from the XML and saved it into a string. which is the format of str = "2009/12/3|19:04:43" & str1 = "2009/12/3|19:05:23" My only concern is how is it possible to compare these two strings by date & time and display the one which is latest. Please let me know the simplest way to compare them. Thanks in Advance

    C C _ 3 Replies Last reply
    0
    • B brucewayn

      I am working for an application where i am supposed to display the latest data on a list box from a XML. I got the datetime node from the XML and saved it into a string. which is the format of str = "2009/12/3|19:04:43" & str1 = "2009/12/3|19:05:23" My only concern is how is it possible to compare these two strings by date & time and display the one which is latest. Please let me know the simplest way to compare them. Thanks in Advance

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

      You could use the COleDateTime::ParseDateTime[^] function in order to "convert" the strings into COleDateTime objects which are comparable. But your string has to be a standard format. Is the format in the xml fixed or could you adapt it a bit ? In yes, then I would suggest to use a standard format and then use COleDateTime objects.

      Cédric Moonen Software developer
      Charting control [v1.5] OpenGL game tutorial in C++

      B 1 Reply Last reply
      0
      • B brucewayn

        I am working for an application where i am supposed to display the latest data on a list box from a XML. I got the datetime node from the XML and saved it into a string. which is the format of str = "2009/12/3|19:04:43" & str1 = "2009/12/3|19:05:23" My only concern is how is it possible to compare these two strings by date & time and display the one which is latest. Please let me know the simplest way to compare them. Thanks in Advance

        C Offline
        C Offline
        cmk
        wrote on last edited by
        #3

        Looks like your strings are in year/month/day|hour:min:sec format. The 19 hour indicates you are using 24 hr notation (rather than 12 hr + am/pm) - good. The only change you need to make is format your day as 03 instead of 3. After that why can't you just do a normal string compare ?

        ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

        B 1 Reply Last reply
        0
        • C Cedric Moonen

          You could use the COleDateTime::ParseDateTime[^] function in order to "convert" the strings into COleDateTime objects which are comparable. But your string has to be a standard format. Is the format in the xml fixed or could you adapt it a bit ? In yes, then I would suggest to use a standard format and then use COleDateTime objects.

          Cédric Moonen Software developer
          Charting control [v1.5] OpenGL game tutorial in C++

          B Offline
          B Offline
          brucewayn
          wrote on last edited by
          #4

          No we are not supposed to change the format. I will be happy if you could provide me the sample code for parsing the date & time from the string and do the comparision. Thanks in advance

          T 1 Reply Last reply
          0
          • C cmk

            Looks like your strings are in year/month/day|hour:min:sec format. The 19 hour indicates you are using 24 hr notation (rather than 12 hr + am/pm) - good. The only change you need to make is format your day as 03 instead of 3. After that why can't you just do a normal string compare ?

            ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

            B Offline
            B Offline
            brucewayn
            wrote on last edited by
            #5

            Normal string comparison can be done however if the values of the strings are changed to str = "2009/12/3|19:04:43" & str1 = "2009/12/3|9:05:23" Then It shows that str1 is greater than str though STRING str is the latest one. Thus i want to have other way where we can compare them by date and time.

            C 1 Reply Last reply
            0
            • B brucewayn

              No we are not supposed to change the format. I will be happy if you could provide me the sample code for parsing the date & time from the string and do the comparision. Thanks in advance

              T Offline
              T Offline
              tasumisra
              wrote on last edited by
              #6

              you need not to change the formate... just change the formate for comparision purpose .. and retain the existing formate....

              vikas da

              1 Reply Last reply
              0
              • B brucewayn

                I am working for an application where i am supposed to display the latest data on a list box from a XML. I got the datetime node from the XML and saved it into a string. which is the format of str = "2009/12/3|19:04:43" & str1 = "2009/12/3|19:05:23" My only concern is how is it possible to compare these two strings by date & time and display the one which is latest. Please let me know the simplest way to compare them. Thanks in Advance

                _ Offline
                _ Offline
                _Superman_
                wrote on last edited by
                #7

                If you cannot change the format and want to know the difference, then extract year and compare first. If they are same, extract month and compare. Continue this for day, hour, minute and second. You can extract by tokenizing the string using the tokens /|: Or you can simply delete the 3 charecters from the string /|: And then assign them to ULONGLONG variables and compare the numbers.

                «_Superman_»

                1 Reply Last reply
                0
                • B brucewayn

                  Normal string comparison can be done however if the values of the strings are changed to str = "2009/12/3|19:04:43" & str1 = "2009/12/3|9:05:23" Then It shows that str1 is greater than str though STRING str is the latest one. Thus i want to have other way where we can compare them by date and time.

                  C Offline
                  C Offline
                  cmk
                  wrote on last edited by
                  #8

                  That's not the same as the original you showed. In your op both were 19: which made me assume you were using 2 digit formatting for everything but day, which is why i said change it. You said you are saving the string so i assume you control the formatting. Just use YYYY/MM/DD|hh:mm:ss and you'll be fine. So for your last example you'd have: str = "2009/12/03|19:04:43" str1 = "2009/12/03|09:05:23"

                  ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

                  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