字符串替换

//字符串替换函数
int ReplaceStr(char *v_sString,const char *vi_sOld,const char *vi_sNew)
{
 int nRet = 0;
 char sTmp[1024]={0};

 if(NULL == v_sString || NULL == vi_sOld || NULL == vi_sNew)
 {
  return nRet;
 }

 if(0 == strlen(v_sString) || 0 == strlen(vi_sOld))
 {
  return nRet;
 }
 
 if(0 == strcmp(vi_sOld,vi_sNew))
 {
  return nRet;
 }

 while(true)
 {
  char *pPosition = NULL;
  pPosition = strstr(v_sString,vi_sOld);

  if(NULL == pPosition)
  {
   break;
  }
  else
  {
   strncpy(sTmp,v_sString,pPosition-v_sString);
   strcat(sTmp,vi_sNew);
   strcat(sTmp,pPosition+strlen(vi_sOld));
   memset(v_sString,0x00,strlen(v_sString));
   strcpy(v_sString,sTmp);
   ++nRet;
  }
 }

 return nRet;
}

你可能感兴趣的:(字符串替换)