📄 replace.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <meta name="generator" content= "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org"> <title>replace</title> <link href="../cppreference.css" rel="stylesheet" type="text/css"></head><body><table> <tr> <td> <div class="body-content"> <div class="header-box"> <a href="../index.html">cppreference.com</a> > <a href= "index.html">C++ Strings</a> > <a href= "replace.html">replace</a> </div> <div class="name-format"> replace </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <string> string& replace( <strong>size_type</strong> index, <strong>size_type</strong> num, const string& str ); string& replace( <strong>size_type</strong> index1, <strong>size_type</strong> num1, const string& str, <strong>size_type</strong> index2, <strong>size_type</strong> num2 ); string& replace( <strong>size_type</strong> index, <strong>size_type</strong> num, const char* str ); string& replace( <strong>size_type</strong> index, <strong>size_type</strong> num1, const char* str, <strong>size_type</strong> num2 ); string& replace( <strong>size_type</strong> index, <strong>size_type</strong> num1, <strong>size_type</strong> num2, char ch ); string& replace( iterator start, iterator end, const string& str ); string& replace( iterator start, iterator end, const char* str ); string& replace( iterator start, iterator end, const char* str, <strong>size_type</strong> num ); string& replace( iterator start, iterator end, <strong>size_type</strong> num, char ch );</pre> <p>The function replace() either:</p> <ul> <li>replaces characters of the current string with up to <em>num</em> characters from <em>str</em>, beginning at <em>index</em>,</li> <li>replaces up to <em>num1</em> characters of the current string (starting at <em>index1</em>) with up to <em>num2</em> characters from <em>str</em> beginning at <em>index2</em>,</li> <li>replaces up to <em>num</em> characters of the current string with characters from <em>str</em>, beginning at <em>index</em> in <em>str</em>,</li> <li>replaces up to <em>num1</em> characters in the current string (beginning at <em>index1</em>) with <em>num2</em> characters from <em>str</em> beginning at <em>index2</em>,</li> <li>replaces up to <em>num1</em> characters in the current string (beginning at <em>index</em>) with <em>num2</em> copies of <em>ch</em>,</li> <li>replaces the characters in the current string from <em>start</em> to <em>end</em> with <em>str</em>,</li> <li>replaces characters in the current string from <em>start</em> to <em>end</em> with <em>num</em> characters from <em>str</em>,</li> <li>or replaces the characters in the current string from <em>start</em> to <em>end</em> with <em>num</em> copies of <em>ch</em>.</li> </ul> <p>For example, the following code displays the string "They say he carved it himself...find your soul-mate, Homer."</p> <pre class="example-code"> string s = "They say he carved it himself...from a BIGGER spoon"; string s2 = "find your soul-mate, Homer."; s.replace( 32, s2.length(), s2 ); cout << s << endl; </pre> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="insert.html">insert</a> </div> </div> </td> </tr> </table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -