replacing char no 2 in a string?

Discussion in 'C++' started by Sonnich Jensen, Oct 12, 2011.

  1. Hi all

    I use MicroSoft Visual studio 2008 and while working with strings, I
    like to change "abcde" into "abXde", in other words always change pos
    2 to X.

    It should be s[2]='X'; but that does not work (no set)

    I tried s=s.Insert(2, "X); but that inserts it there, then I can
    remove it. Replace will look for a string and replace that part. But
    how do I do that for a single static char?

    Seems to me like Microsoft C++ is slight different from what I learned
    15 years ago...

    Sonnich
    Sonnich Jensen, Oct 12, 2011
    #1
    1. Advertising

  2. On Oct 12, 11:30 am, Sonnich Jensen <> wrote:
    > Hi all
    >
    > I use MicroSoft Visual studio 2008 and while working with strings, I
    > like to change "abcde" into "abXde", in other words always change pos
    > 2 to X.
    >
    > It should be s[2]='X'; but that does not work (no set)



    It isn't clear exactly what you mean by "strings", but s[2]='X';
    should work in any of the following cases:

    // Make a modifiable char array, size 6, on the stack
    char foo[] = "abcde";
    foo[2] = 'X';

    // Allocate six chars on the heap
    char * foo = new char[6];
    std::strcpy(foo, "abcde");
    foo[2] = 'X';

    // Make an std::string object on the stack
    std::string foo("abcde");
    foo[2] = 'X';


    This will compile (hopefully with a warning) but will result in
    undefined behavior:

    // Create a pointer to a 6-character string constant
    char * foo = "abcde";
    foo[2] = 'X'; // will likely crash: you can't edit a string constant!


    > I tried s=s.Insert(2, "X); but that inserts it there, then I can
    > remove it. Replace will look for a string and replace that part. But
    > how do I do that for a single static char?


    std::string does not have a member named Insert() with that
    capitalization. So presumably you are using some other kind of string
    class. It would be helpful if you said which one!

    - Kevin
    Kevin McCarty, Oct 12, 2011
    #2
    1. Advertising

  3. Sonnich Jensen

    Goran Guest

    On Oct 12, 8:30 pm, Sonnich Jensen <> wrote:
    > Hi all
    >
    > I use MicroSoft Visual studio 2008 and while working with strings, I
    > like to change "abcde" into "abXde", in other words always change pos
    > 2 to X.


    You are working with class called CStringA or CStringW. There's a
    typedef to get one of them called CString.

    (BTW, if you do use CString, you are asking a library-specific
    question. Therefore, you should go to a library-specific group, e.g.
    http://social.msdn.microsoft.com/Forums/en/vcmfcatl).

    > It should be s[2]='X'; but that does not work (no set)


    It does not compile. Not same as "not work".

    >
    > I tried s=s.Insert(2, "X); but that inserts it there, then I can
    > remove it. Replace will look for a string and replace that part. But
    > how do I do that for a single static char?


    SetAt. It's documented.

    Goran.
    Goran, Oct 13, 2011
    #3
  4. Sonnich Jensen

    Arne Mertz Guest

    On Oct 12, 8:30 pm, Sonnich Jensen <> wrote:
    >
    > It should be s[2]='X'; but that does not work (no set)
    >


    be sure that
    1) you are not working on a copy of the string you inspect later
    2) operator[] of your string class really returns a reference to the
    string content, not something else.

    Best shot to realize what happens and why the 'X' doesn't show up in
    your original string ist to debug the whole thing.

    HTH
    Arne
    Arne Mertz, Oct 13, 2011
    #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. wwj
    Replies:
    7
    Views:
    550
  2. Tim Quon

    replacing a string in a char pointer

    Tim Quon, Sep 10, 2003, in forum: C Programming
    Replies:
    5
    Views:
    643
    Dan Pop
    Sep 10, 2003
  3. wwj
    Replies:
    24
    Views:
    2,507
    Mike Wahler
    Nov 7, 2003
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,046
    Ian Collins
    May 9, 2006
  5. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    274
    Chris Hohmann
    Apr 11, 2005
Loading...

Share This Page