The replace function of the STL string class does not work as expected
Article ID: 168377
Article Last Modified on 9/2/2005
APPLIES TO
- The Standard C++ Library, when used with:
- Microsoft Visual C++ 4.2 Enterprise Edition
- Microsoft Visual C++ 4.2 Professional Edition
- Microsoft Visual C++ 5.0 Enterprise Edition
- Microsoft Visual C++ 5.0 Professional Edition
- Microsoft Visual C++ 6.0 Enterprise Edition
- Microsoft Visual C++ 6.0 Professional Edition
- Microsoft Visual C++ 6.0 Standard Edition
This article was previously published under Q168377
SYMPTOMS
The string::replace member function does not work as expected. This is
because the same string object is used for modification and input. Many
data manipulation routines do not check for overlapping or identical
arguments, causing unexpected results.
RESOLUTION
Create a temporary copy of the string as shown below:
s1.replace (1,2,string("c"));
STATUS
This behavior is by design.
Keywords: kbtshoot kbcodesnippet kbprogramming kbprb kbsample kbcode KB168377