Replace in C ++ string

Source: Internet
Author: User

Replace the element or substring in the original string. Returns the string after replacement.

(1) Use string or C-string to replace the _ num1 character starting from _ pos1 in the operation string.

Basic _ string & replace (size _ type _ pos1, size _ type _ num1, const value _ type * _ PTR );

Basic _ string & replace (size _ type _ pos1, size _ type _ num1, const Basic _ string _ Str );


String A, B;

String S ("aaaaaaaa ");

String s1p ("BBB ");

Const char * cs1p = "CCC ";

A = S. Replace (1, 3, s1p); // s = "abbbaaaa"

B = S. Replace (5, 3, cs1p );//

 

(2) Use the _ num2 characters starting from _ pos2 in string to replace _ num1 characters starting from _ pos1 in string.

Use _ num2 characters in C-string to replace _ num1 characters starting from _ pos1 in string.

Basic _ string & replace (size _ type _ pos1, size _ type _ num1, const Basic _ string & _ STR,

Size _ type _ pos2, size _ type );

Basic _ string & replace (size _ type _ pos1, size _ type _ num1,

Const value _ type * _ PTR, size _ type _ num2 );


String A, B;

String S ("aaaaaaaa ");

String S2P ("BBB ");

Const char * cs2p = "CCC ";

A = S. Replace (1, 3, S2P, 1, 2); // s = "abbaaaa"

B = S. Replace (4, 3, cs2p, 1 );//

 

(3) Use _ count character _ CH to replace _ num1 characters starting from _ pos1 in the operation string.

Basic _ string & replace (size _ type _ pos1, size _ type _ num1,

Size _ type _ count, value _ type _ CH );

 

String result;

String S ("aaaaaaaa ");

Char CH = 'C ';

Result = S. Replace (1, 3, 4, ch); // s = "accccaaaa"

 

(4) use string or C-string to replace the characters from first0 to last0 in the string operation.

Basic _ string & replace (iterator first0, iterator last0, const Basic _ string & _ Str );

Basic _ string & replace (iterator first0, iterator _ last0, const value _ type * _ PTR );


String S ("aaaaaaaa"); string s4p ("BBB ");

Const char * cs4p = "CCC ";

Basic_string <char >:: iterator iterf0, iterl0;

Iterf0 = S. Begin (); iterl0 = S. Begin () + 3;

String A, B;

A = S. Replace (iterf0, iterl0, s4p); // s = "bbbaaaaa"

B = S. Replace (iterf0, iterl0, cs4p );//

 

(5) use the _ num2 characters starting from _ pos2 in string to replace the characters from first0 to last0 in string.

Use the _ num2 characters in the C-string to replace the characters from first0 to last0 in the operation string.

Basic _ string & replace (iterator _ first0, iterator _ last0,

Const value _ type * _ PTR, size _ type _ num2 );

Template <class inputiterator> Basic _ string & replace (

Iterator _ first0, iterator _ last0,

Inputiterator _ first, inputiterator _ last );

Iterf3 = S. Begin () + 1; iterl3 = S. Begin () + 3;

Iterf4 = S. Begin (); iterl4 = S. Begin () + 2;

A = S. Replace (iterf3, iterl3, iterf4, iterl4 );

B = S. Replace (iterf1, iterl1, cs5 P, 4 );

(6) Use _ count character _ CH to replace the characters from first0 to last0 in the operation string.

Basic _ string & replace (iterator _ first0, iterator _ last0,

Size _ type _ count, value _ type _ CH );

A = S. Replace (iterf2, iterl2, 4, ch );

 

Frequently Used functions:

// Test1.cpp: defines the entry point of the console application. <Br/> // </P> <p> # include "stdafx. H "<br/> # include <iostream> <br/> # include <string> <br/> using namespace STD; </P> <p> string & replace_all (string & STR, const string & old_value, const string & new_value) <br/>{< br/> while (true) {<br/> string: size_type pos (0); <br/> If (Pos = Str. find (old_value ))! = String: NPOs) <br/> Str. replace (Pos, old_value.length (), new_value); <br/> else break; <br/>}< br/> return STR; <br/>}</P> <p> string & replace_all_distinct (string & STR, const string & old_value, const string & new_value) <br/>{< br/> for (string: size_type pos (0); pos! = String: NPOs; POS + = new_value.length () {<br/> If (Pos = Str. Find (old_value, POS ))! = String: NPOs) <br/> Str. replace (Pos, old_value.length (), new_value); <br/> else break; <br/>}< br/> return STR; <br/>}</P> <p> int main () <br/> {<br/> // cout <STR; <br/> string STR = "lihan"; <br/> Str. replace (Str. begin (), str. end ()-12212,'s '); <br/> cout <STR; <br/> cout <replace_all (string (""), "2 ", "3") <Endl; <br/> cout <replace_all_distinct (string ("12212"), "12", "21") <Endl; <br/>}</P> <p>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.