C++ string question

Discussion in 'OT Technology' started by dinger, Feb 8, 2003.

  1. dinger

    dinger Guest

    Hi,

    Suppose I have the following code:

    Code:
    string word1;
    string word2;
    
    word2 = "qwerty";
    
    How do I copy word2 to word1 so that (word1=="qwerty")==true, without converting the strings to char arrays? Also, I need to copy the value of word2 into word1, not have word1 point at word2.

    Thanks!
     
  2. AznRyda

    AznRyda キモかわいい!

    Joined:
    May 6, 2000
    Messages:
    28,632
    Likes Received:
    0
    Location:
    残業中
    strcpy
     
  3. dinger

    dinger Guest

    Thanks
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    didn't you not want to convert to char arrays?
    and, aren't those STL strings?

    word1 = word2;
     
  5. dinger

    dinger Guest

    Yeah, those are STL strings... strcpy() didn't work but I figured it out:)

    word2 = word1.substr(0, word1.length());

    I tried word2=word1 but that seemed to set word2 to point to word1.
     
  6. Chimpa Codigo

    Chimpa Codigo Bаnned bу Ѕuреr Modulators

    Joined:
    Jan 27, 2001
    Messages:
    68,463
    Likes Received:
    21
    Location:
    Salinas, CA
    i thought it would be word1 = word2 or word1 +=word2
     
  7. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Also, if you don't feel a strong need to declare all your variables at the same time, couldn't you do string word2(word1);? Or does that only work with char arrays?
     
  8. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    i'm not familiar with the STL strings, but shouldn't they have a copy contructor?
     

Share This Page