StringReplace
StringReplace
该函数使用一组符号替换字符串中所有找到的子字符串。
int StringReplace(
string& str, // the string in which substrings will be replaced
const string find, // the searched substring
const string replacement // the substring that will be inserted to the found positions
);参数
- str
[in][out] 要替换子字符串的字符串。
- find
[in] 需要替换的子字符串。
- replacement
[in] 将替代找到的子字符串的字符串。
返回值
如果成功,该函数返回替换次数;否则返回-1。如果出现错误,请调用GetLastError()函数。
注意
如果函数成功运行但未进行任何替换(未找到要替换的子字符串),则返回0。
错误可能是由于str或find参数不正确引起的(空或非初始化的字符串,请参阅StringInit())。此外,如果内存不足无法完成替换,也会发生错误。
示例:
string text="The quick brown fox jumped over the lazy dog.";
int replaced=StringReplace(text,"quick","slow");
replaced+=StringReplace(text,"brown","black");
replaced+=StringReplace(text,"fox","bear");
Print("Replaced: ", replaced,". Result=",text);
// Result
// Replaced: 3. Result=The slow black bear jumped over the lazy dog.
//
另请参阅
最后更新于