The replace function of the STL string class does not work as expected (168377)
The information in this article applies to:
- The Standard C++ Library, when used with:
- Microsoft Visual C++, 32-bit Enterprise Edition 4.2
- Microsoft Visual C++, 32-bit Professional Edition 4.2
- Microsoft Visual C++, 32-bit Enterprise Edition 5.0
- Microsoft Visual C++, 32-bit Professional Edition 5.0
- Microsoft Visual C++, 32-bit Enterprise Edition 6.0
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- Microsoft Visual C++, 32-bit Learning Edition 6.0
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.
| Modification Type: | Major | Last Reviewed: | 9/2/2005 |
|---|
| Keywords: | kbSample kbCodeSnippet kbcode kbProgramming kbtshoot kbprb KB168377 kbAudDeveloper |
|---|
|