由于LINUX C没有对字符串子串替换功能,所以我自己写了一个str_replace函数,实现了字符串替换.
请大家参考.
/*
* FUNCTION : str_replace
* ABSTRACT : replace child string in a string.
* PARAMETER :
* char* str the string that be replace
* char* str_src source string
* char* str_des destination string
* RETURN :
* 0 OK
* -1 FALSE
* CREATE : 2006-01-05 ZHANG.JINCUN
* NOTE :
*/
int str_replace(char* str,char* str_src, char* str_des){
char *ptr=NULL;
char buff[256];
char buff2[256];
int i = 0;
if(str != NULL){
strcpy(buff2, str);
}else{
printf("str_replace err!/n");
return -1;
}
memset(buff, 0x00, sizeof(buff));
while((ptr = strstr( buff2, str_src)) !=0){
if(ptr-buff2 != 0) memcpy(&buff[i], buff2, ptr - buff2);
memcpy(&buff[i + ptr - buff2], str_des, strlen(str_des));
i += ptr - buff2 + strlen(str_des);
strcpy(buff2, ptr + strlen(str_src));
}
strcat(buff,buff2);
strcpy(str,buff);
return 0;
}
调用方法:
char sztmp[256];
strcpy(sztmp,"googasdf001jlkasdj001goog goog");
str_replace(sztmp, "001", "good");
printf("%s/n",sztmp);
打印结果:
googasdfgoodjlkasdjgoodgoog goog