char array to string?

Discussion in 'C++' started by pentiumPunk, Aug 31, 2003.

  1. pentiumPunk

    pentiumPunk Guest

    Hi, I created an array of characters and want to convert that array to a
    string.
    I want to do something like this :

    char array1 [25]; //with letters already in it
    string string1;
    string1 = array1;

    That wont work will it? what would be the proper syntax? thanks in ahead!
     
    pentiumPunk, Aug 31, 2003
    #1
    1. Advertising

  2. pentiumPunk

    Noah Roberts Guest

    pentiumPunk wrote:
    > Hi, I created an array of characters and want to convert that array to a
    > string.
    > I want to do something like this :
    >
    > char array1 [25]; //with letters already in it
    > string string1;


    Change to "string string1(array);" assuming that your char array is null
    terminated.

    NR
     
    Noah Roberts, Aug 31, 2003
    #2
    1. Advertising

  3. "pentiumPunk" <> wrote in message
    news:Tla4b.38316$...
    > Hi, I created an array of characters and want to convert that array to a
    > string.
    > I want to do something like this :
    >
    > char array1 [25]; //with letters already in it
    > string string1;
    > string1 = array1;
    >
    > That wont work will it? what would be the proper syntax? thanks in ahead!


    If the string in array1 is null-terminated:
    string1.assign( array1 );
    or, better, direct initialization:
    string string1( array1 );

    If array1 is not null-terminated:
    string1.assign( array1, array1+25 );
    or: string1.assign( array1, 25 );
    And there is a pair of matching constructors that
    accept the same parameters:
    string string1( array1, array1+25 );
    or: string string1( array1, 25 );

    Too much choice indeed...

    hth
    --
    Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Aug 31, 2003
    #3
  4. "pentiumPunk" <> wrote in message
    news:Tla4b.38316$...
    > Hi, I created an array of characters and want to convert that array to a
    > string.
    > I want to do something like this :
    >
    > char array1 [25]; //with letters already in it
    > string string1;
    > string1 = array1;
    >
    > That wont work will it?


    Why not? C++ is easier than you think.

    > what would be the proper syntax? thanks in ahead!
    >


    Very slightly better is to construct the string instead of assigning to it.

    char array1 [25]; //with letters already in it
    string string1(array1);

    I'm assuming that array1 is null terminated. If not you should write

    char array1 [25]; // with 25 letters already in it
    string string1(array1, 25);

    john
     
    John Harrison, Aug 31, 2003
    #4
  5. "pentiumPunk" <> wrote in message news:<Tla4b.38316$>...

    > char array1 [25]; //with letters already in it
    > string string1;
    > string1 = array1;
    >
    > That wont work will it? what would be the proper syntax? thanks in ahead!


    Yes, that will work... Didn't you try it before posting here?

    - Shane
     
    Shane Beasley, Aug 31, 2003
    #5
    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. wwj
    Replies:
    7
    Views:
    558
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,058
    Ian Collins
    May 9, 2006
  3. Replies:
    3
    Views:
    737
  4. davidb
    Replies:
    0
    Views:
    764
    davidb
    Sep 1, 2006
  5. davidb
    Replies:
    6
    Views:
    1,554
    Default User
    Sep 1, 2006
Loading...

Share This Page