String string replacement

Source: Internet
Author: User

Address: http://www.vimer.cn/2009/11/string%E6%9B%BF%E6%8D%A2%E6%89%80%E6%9C%89%E6%8C%87%E5% AE %9A%E5%AD%97%E7%AC%A6%E4%B8%B2%EF%BC%88c%EF%BC%89.html

 # Include  <  String  >  

# Include < Iostream >

Using Namespace STD;

String Replace_all ( String STR, Const String Old_value, Const String New_value)

{

While ( True ){

String : Size_type pos ( 0 );

If (Pos = Str. Find (old_value )) ! = String : NPOs)

Str. Replace (Pos, old_value.length (), new_value );

Else Break ;

}

Return STR;

}

String Replace_all_distinct ( String STR, Const String Old_value, Const String New_value)

{

For ( String : Size_type pos ( 0 ); POS ! = String : NPOs; POS + = New_value.length ()){

If (Pos = Str. Find (old_value, POS )) ! = String : NPOs)

Str. Replace (Pos, old_value.length (), new_value );

Else Break ;

}

Return STR;

}

Int Main ()

{

Cout < Replace_all ( String ( " 12212 " ), " 12 " , " 21 " ) < Endl;

Cout < Replace_all_distinct ( String ( " 12212 " ), " 12 " , " 21 " ) < Endl;

End

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.