replacing char no 2 in a string?

S

Sonnich Jensen

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
 
K

Kevin McCarty

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
 
G

Goran

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.
 
A

Arne Mertz

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top