Initialize std::string with character array

Discussion in 'C++' started by Jim Langston, Nov 21, 2006.

  1. Jim Langston

    Jim Langston Guest

    Is it possible to initialize a std::string with a character array, not
    neccessarily null terminated? I.E. Given something like this:

    char buffer[5];
    buffer[0] = 0x01;
    buffer[1] = 0x00;
    buffer[2] = 'A';
    buffer[3] = 'B';
    buffer[4] = 'C';

    The only way I know to do it now is to create a std::string and with a for
    loop add each character. Is there some other, better way? I mean, I would
    love to be able to say:

    std::string Buffer( buffer, 5 );

    Oh, you gotta be joking me! I just tried that just for the heck of it, and
    it compiles and does exactly as I expect! o_O
    Jim Langston, Nov 21, 2006
    #1
    1. Advertising

  2. Jim Langston

    dasjotre Guest

    Jim Langston wrote:
    > Is it possible to initialize a std::string with a character array, not
    > neccessarily null terminated? I.E. Given something like this:
    >
    > char buffer[5];
    > buffer[0] = 0x01;
    > buffer[1] = 0x00;
    > buffer[2] = 'A';
    > buffer[3] = 'B';
    > buffer[4] = 'C';
    >
    > The only way I know to do it now is to create a std::string and with a for
    > loop add each character. Is there some other, better way? I mean, I would
    > love to be able to say:
    >
    > std::string Buffer( buffer, 5 );
    >
    > Oh, you gotta be joking me! I just tried that just for the heck of it, and
    > it compiles and does exactly as I expect! o_O


    std::string s(buffer, buffer+5);

    in your case it will be a 1 character long string with
    SOH character only
    dasjotre, Nov 21, 2006
    #2
    1. Advertising

  3. Jim Langston

    Pete Becker Guest

    dasjotre wrote:
    > Jim Langston wrote:
    >> Is it possible to initialize a std::string with a character array, not
    >> neccessarily null terminated? I.E. Given something like this:
    >>
    >> char buffer[5];
    >> buffer[0] = 0x01;
    >> buffer[1] = 0x00;
    >> buffer[2] = 'A';
    >> buffer[3] = 'B';
    >> buffer[4] = 'C';
    >>
    >> The only way I know to do it now is to create a std::string and with a for
    >> loop add each character. Is there some other, better way? I mean, I would
    >> love to be able to say:
    >>
    >> std::string Buffer( buffer, 5 );
    >>
    >> Oh, you gotta be joking me! I just tried that just for the heck of it, and
    >> it compiles and does exactly as I expect! o_O

    >
    > std::string s(buffer, buffer+5);
    >
    > in your case it will be a 1 character long string with
    > SOH character only
    >


    Buffer(buffer, 5) constructs a string object that holds the first five
    characters in buffer.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, Nov 21, 2006
    #3
  4. Jim Langston

    Pete Becker Guest

    Jim Langston wrote:
    > Is it possible to initialize a std::string with a character array, not
    > neccessarily null terminated? I.E. Given something like this:
    >
    > char buffer[5];
    > buffer[0] = 0x01;
    > buffer[1] = 0x00;
    > buffer[2] = 'A';
    > buffer[3] = 'B';
    > buffer[4] = 'C';
    >
    > The only way I know to do it now is to create a std::string and with a for
    > loop add each character. Is there some other, better way? I mean, I would
    > love to be able to say:
    >
    > std::string Buffer( buffer, 5 );
    >
    > Oh, you gotta be joking me! I just tried that just for the heck of it, and
    > it compiles and does exactly as I expect! o_O
    >


    No need to be surprised. Just read the documentation.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, Nov 21, 2006
    #4
    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 Jansson
    Replies:
    5
    Views:
    6,301
    Ivan Vecerina
    Mar 17, 2005
  2. Fei Liu
    Replies:
    9
    Views:
    441
  3. Jeffrey Walton
    Replies:
    10
    Views:
    938
    Mathias Gaunard
    Nov 26, 2006
  4. herman
    Replies:
    5
    Views:
    7,599
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 30, 2007
  5. Replies:
    10
    Views:
    776
    Jerry Coffin
    Jan 18, 2008
Loading...

Share This Page