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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Scanning & extracting from CString

Scanning & extracting from CString

Scheduled Pinned Locked Moved C / C++ / MFC
4 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.
  • J Offline
    J Offline
    Jose Luis Sogorb
    wrote on last edited by
    #1

    Anybody can show me an easy way to extract words from a string. Say I have the CString "03 March 2005" and I want an integer with the day, a string with month and an integer with year. Thanks.

    D 1 Reply Last reply
    0
    • J Jose Luis Sogorb

      Anybody can show me an easy way to extract words from a string. Say I have the CString "03 March 2005" and I want an integer with the day, a string with month and an integer with year. Thanks.

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

      Several ways:

      CString str = "03 March 2005";
      int nDay = atoi(str); // stops at the first non-numeric character
      int nYear = atoi(str.Right(4));
      or
      int nYear;
      char szMonth[6];
      sscanf(str, "%d %s %d", &nDay, szMonth, &nYear);
      or
      CString strMonth = str.Mid(3, 5);
      or
      int nSpace1 = str.Find(' ');
      int nSpace2 = str.ReverseFind(' ');
      strMonth = str.Mid(nSpace1 + 1, nSpace2 - nSpace1 - 1);

      There are probably more, but I think you get the idea.


      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

      B J 2 Replies Last reply
      0
      • D David Crow

        Several ways:

        CString str = "03 March 2005";
        int nDay = atoi(str); // stops at the first non-numeric character
        int nYear = atoi(str.Right(4));
        or
        int nYear;
        char szMonth[6];
        sscanf(str, "%d %s %d", &nDay, szMonth, &nYear);
        or
        CString strMonth = str.Mid(3, 5);
        or
        int nSpace1 = str.Find(' ');
        int nSpace2 = str.ReverseFind(' ');
        strMonth = str.Mid(nSpace1 + 1, nSpace2 - nSpace1 - 1);

        There are probably more, but I think you get the idea.


        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

        B Offline
        B Offline
        Blake Miller
        wrote on last edited by
        #3

        sscanf is evil! I like to use it for buffer overruns myself :mad:

        1 Reply Last reply
        0
        • D David Crow

          Several ways:

          CString str = "03 March 2005";
          int nDay = atoi(str); // stops at the first non-numeric character
          int nYear = atoi(str.Right(4));
          or
          int nYear;
          char szMonth[6];
          sscanf(str, "%d %s %d", &nDay, szMonth, &nYear);
          or
          CString strMonth = str.Mid(3, 5);
          or
          int nSpace1 = str.Find(' ');
          int nSpace2 = str.ReverseFind(' ');
          strMonth = str.Mid(nSpace1 + 1, nSpace2 - nSpace1 - 1);

          There are probably more, but I think you get the idea.


          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          J Offline
          J Offline
          Jose Luis Sogorb
          wrote on last edited by
          #4

          Thank you!

          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