Help splitting a simple date string

Discussion in 'C++' started by yogi_bear_79, Mar 4, 2008.

  1. yogi_bear_79

    yogi_bear_79 Guest

    I have a simple string (i.e. February 27, 2008) that I need to split
    into three parts. The month, day, and year. Splitting into a string
    array would work, and I could convert day and years to integers
    later. I've bene looking around, and everything I see seems more
    complicated than it should be! Help!
    yogi_bear_79, Mar 4, 2008
    #1
    1. Advertising

  2. yogi_bear_79

    Daniel T. Guest

    On Mar 3, 8:49 pm, yogi_bear_79 <> wrote:
    > I have a simple string (i.e. February 27, 2008) that I need to split
    > into three parts. The month, day, and year.  Splitting into a string
    > array would work, and I could convert day and years to integers
    > later.  I've bene looking around, and everything I see seems more
    > complicated than it should be! Help!


    If you know the format is exactly as you show above, then it is a
    simple matter of finding the spaces.

    vector<string> splitDate( const string& str ) {
    vector<string> result;
    string::size_type i = str.find( ' ' );
    result.push_back( str.substr( 0, i ) );
    string::size_type last = i;
    i = str.find( ' ', i + 1 );
    result.push_back( str.substr( last, i - last ) );
    result.push_back( str.substr( i ) );
    return result;
    }

    If you turn the above into your teacher though, he should grade you
    very poorly. You really should have some error checking and probably
    some sort of verification code.

    But the above should at least get you started.
    Daniel T., Mar 4, 2008
    #2
    1. Advertising

  3. yogi_bear_79

    yogi_bear_79 Guest

    On Mar 3, 9:08 pm, "Daniel T." <> wrote:
    > On Mar 3, 8:49 pm, yogi_bear_79 <> wrote:
    >
    > > I have a simple string (i.e. February 27, 2008) that I need to split
    > > into three parts. The month, day, and year.  Splitting into a string
    > > array would work, and I could convert day and years to integers
    > > later.  I've bene looking around, and everything I see seems more
    > > complicated than it should be! Help!

    >
    > If you know the format is exactly as you show above, then it is a
    > simple matter of finding the spaces.
    >
    >    vector<string> splitDate( const string& str ) {
    >       vector<string> result;
    >       string::size_type i = str.find( ' ' );
    >       result.push_back( str.substr( 0, i ) );
    >       string::size_type last = i;
    >       i = str.find( ' ', i + 1 );
    >       result.push_back( str.substr( last, i - last ) );
    >       result.push_back( str.substr( i ) );
    >       return result;
    >    }
    >
    > If you turn the above into your teacher though, he should grade you
    > very poorly. You really should have some error checking and probably
    > some sort of verification code.
    >
    > But the above should at least get you started.



    Thank you so much for the idea. I do believe that it is slightly more
    advanced than I shoul dbe at this point. I've settled on the code
    below, just as soon as I parrse out the comma!

    char longDate[100];
    char* nextToken;

    cout << " \n Enter a date in the following format(February 27,
    2008):";
    cin.getline(longDate,100);

    char *month = strtok_s(longDate, " ", &nextToken);
    char *day = strtok_s(NULL, ",", &nextToken);
    char *year = strtok_s(NULL, "\0", &nextToken);
    yogi_bear_79, Mar 4, 2008
    #3
  4. * yogi_bear_79:
    > I have a simple string (i.e. February 27, 2008) that I need to split
    > into three parts. The month, day, and year. Splitting into a string
    > array would work, and I could convert day and years to integers
    > later. I've bene looking around, and everything I see seems more
    > complicated than it should be! Help!


    Many ways. One has already been illustrated by Daniel T else-thread,
    using built-in std::string functions. The simplest would possibly be to
    use a regular expression, but then you need to know about those.

    Somewhere in the middle (disclaimer: code not touched by compiler's hands):

    std::vector<std::string> splitOnWhitespace( std::string const& s )
    {
    std::istringstream stream( s );
    std::string component;
    std::vector<std::string> result;

    while( s >> component ) { result.push_back( component ); }
    return result;
    }

    Here I just let delimiters such as that comma hang on. They'll be
    removed by conversion to numeric later.

    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Mar 4, 2008
    #4
  5. yogi_bear_79

    Jeff Schwab Guest

    yogi_bear_79 wrote:
    > I have a simple string (i.e. February 27, 2008) that I need to split
    > into three parts. The month, day, and year. Splitting into a string
    > array would work, and I could convert day and years to integers
    > later. I've bene looking around, and everything I see seems more
    > complicated than it should be! Help!


    #include <cassert>
    #include <iterator>
    #include <sstream>
    #include <string>
    #include <vector>

    int main() {

    std::istringstream stream("February 27, 2008");
    std::istream_iterator<std::string> in(stream), end;
    std::vector<std::string> strings(in, end);

    assert(strings.size() == 3);
    assert(strings.at(0) == "February");
    assert(strings.at(1) == "27,");
    assert(strings.at(2) == "2008");
    }
    Jeff Schwab, Mar 4, 2008
    #5
  6. yogi_bear_79

    Guest

    Hello,

    Small suggestion; IMHO, instead of falling back to char* and strtok it
    would be beneficial in the longer run to work with std::string and
    "more advanced" topics. If you are doing this for a school assignment
    where you have freedom and a couple of hours to spare, have a look at
    boost like libraries which would do interesting stuff for you.

    You already have most of the solutions listed anyways.

    Cheers,
    ~M
    , Mar 4, 2008
    #6
  7. yogi_bear_79

    Daniel T. Guest

    On Mar 3, 9:53 pm, yogi_bear_79 <> wrote:
    > On Mar 3, 9:08 pm, "Daniel T." <> wrote:
    >
    >
    >
    >
    >
    > > On Mar 3, 8:49 pm, yogi_bear_79 <> wrote:

    >
    > > > I have a simple string (i.e. February 27, 2008) that I need to split
    > > > into three parts. The month, day, and year.  Splitting into a string
    > > > array would work, and I could convert day and years to integers
    > > > later.  I've bene looking around, and everything I see seems more
    > > > complicated than it should be! Help!

    >
    > > If you know the format is exactly as you show above, then it is a
    > > simple matter of finding the spaces.

    >
    > >    vector<string> splitDate( const string& str ) {
    > >       vector<string> result;
    > >       string::size_type i = str.find( ' ' );
    > >       result.push_back( str.substr( 0, i ) );
    > >       string::size_type last = i;
    > >       i = str.find( ' ', i + 1 );
    > >       result.push_back( str.substr( last, i - last ) );
    > >       result.push_back( str.substr( i ) );
    > >       return result;
    > >    }

    >
    > > If you turn the above into your teacher though, he should grade you
    > > very poorly. You really should have some error checking and probably
    > > some sort of verification code.

    >
    > > But the above should at least get you started.

    >
    > Thank you so much for the idea. I do believe that it is slightly more
    > advanced than I shoul dbe at this point. I've settled on the code
    > below, just as soon as I parrse out the comma!
    >
    >         char longDate[100];
    >         char* nextToken;
    >
    >         cout << " \n Enter a date in the following format(February 27,
    > 2008):";
    >         cin.getline(longDate,100);
    >
    >         char *month = strtok_s(longDate, " ", &nextToken);
    >         char *day = strtok_s(NULL, ",", &nextToken);
    >         char *year = strtok_s(NULL, "\0", &nextToken);- Hide quoted text -
    >
    > }


    The code you show above is considered much more advanced than what I
    showed...
    Daniel T., Mar 4, 2008
    #7
  8. yogi_bear_79

    David Harmon Guest

    On Mon, 3 Mar 2008 17:49:40 -0800 (PST) in comp.lang.c++,
    yogi_bear_79 <> wrote,
    >I have a simple string (i.e. February 27, 2008) that I need to split
    >into three parts. The month, day, and year.


    My version:
    http://groups.google.com/groups?selm=
    David Harmon, Mar 5, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,229
    Michael Borgwardt
    May 30, 2004
  2. John Ericson
    Replies:
    0
    Views:
    419
    John Ericson
    Jul 19, 2003
  3. Mark
    Replies:
    0
    Views:
    434
  4. John Dibling
    Replies:
    0
    Views:
    405
    John Dibling
    Jul 19, 2003
  5. davyb
    Replies:
    7
    Views:
    199
    Gregory Brown
    Nov 18, 2005
Loading...

Share This Page