2.2.3.15.30 string::ReplaceReplace
Description
Replace a character with another.(Comparison is case-sensitive.)
This overloaded member function replaces instances of the substring lpszOld with instances of the string lpszNew. Set comparison is case-sensitive or not by argument bCaseSensitive.
This overloaded member function replaces instances of the all characters as specified by lpszOneOf with the character chNew. If the resulted string has repeated occurance of chNew, they are stripped so that only one is left
Syntax
int Replace( char chOld, char chNew )
int Replace( LPCSTR lpszOld, LPCSTR lpszNew, BOOL bCaseSensitive = TRUE )
int Replace( LPCSTR lpszOneOf, char chNew )
Parameters
- chOld
- [input]The character to be replaced by chNew.
- chNew
- [input]The character replacing chOld.
- lpszOld
- [modify]A pointer to a string containing the characters to be replaced by lpszNew.
- lpszNew
- [input]A pointer to a string containing the characters replacing lpszOld.
- bCaseSensitive
- [input]Perform a case-insensitive searching if TRUE
- lpszOneOf
- [modify] A pointer to a string containing the characters that all are to be replaced by chNew.
- chNew
- [input]The character replacing lpszOneOf.
Return
The number of replaced instances of the character. Zero if the string isn't changed.
The number of replaced instances of the character. Zero if the string isn't changed.
The number of replaced instances of the chNew. Zero if no replacement took place.
Examples
EX1
void string_Replace_ex1()
{
string str("A+B+C+");
int nRet = str.Replace('+','-');
out_int("", nRet);//3
out_str(str);//"A-B-C-"
}
EX2
void string_Replace_ex2()
{
string str("Today is Friday");
char buffer[6] = "Friday";
int nRet = str.Replace(buffer,"Monday");
out_int("", nRet);//1
out_str(str);//"Today is Monday"
}
EX3
void string_Replace_ex3()
{
string str("123 \t456, 789;\n\r1212");
int nRet = str.Replace(" \t\n\r,;",',');
out_int("", nRet);//6
out_str(str);//"123,456,789,1212"
}
Remark
See Also
string::Remove, string::Find
Header to Include
origin.h
|