Use C ++ to replace Replace strings similar to VB

Source: Internet
Author: User

Anyone who has used VB knows that the replace in VB is very useful. It seems that there is no ready-made function in C ++ to implement this function (except cstring in MFC). Let's write it by ourselves.CodeImplement the function.

The C ++ implementation is as follows:

# Include <iostream>
# Include <string>
Using namespace STD;
String Replace (string a, string S, string d ){
While (A. Find (s )! = String: NPOs)
A. Replace (A. Find (s), S. Length (), d );
Return;

}
Main (){
Cout <Replace ("asdfgsdh", "SD", "1123 ");

}

 

C LanguageThe implementation is as follows:

 

# Include <iostream>
# Include <string>
Using namespace STD;
Void R (char * s, char * S1, char * S2, char * S3 ){
Char * Ss = s;
Char * P;
P = strstr (SS, S1 );
If (P = NULL ){
S3 = 0;
Return;
}
While (P! = NULL ){
Int n = p-ss;

Strncat (S3, SS, N );
 
Strcat (S3, S2 );
 
Ss = P + strlen (S1 );
 
P = strstr (SS, S1 );

}
Strcat (S3, SS );
}

Main (){
Char * A = "asdasasdsd ";
Char s [30] = {0 };
R (A, "ASD", "123ui", S );
Cout <s;

 
}

 

the code is relatively simple. If you have nothing to say about it, you should be a trainer.

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.