haitao:
				
 找到一个,会重新分配内存的。。。其实我不需要重新分配内存,原来的空间就是非常大的,所以希望直接在上面搬移。。。
 
			[阅读: 448] 2006-07-05 03:04:31
			
				LPTSTR StrReplaceStr(LPTSTR src,LPTSTR strFind,LPTSTR strReplace)
{
    if(strlen(src)==0) return src;
    if(strlen(strFind)==0) return src;
    SafeBuffer sWrkStr(10240);   // 一个类,自动分配和释放内存
    LPTSTR pNew     = src;
    LPTSTR pOld     = src;
    int    nSize    = 0;
    int    nAddSize = 0;
    int    nOldSize = strlen(strFind);
    int    nRepSize = strlen(strReplace);
    while((pNew = strstr(pOld,strFind))!=NULL)
    {
        nAddSize = pNew - pOld;
        if(nSize + nAddSize + nRepSize > sWrkStr.GetLength())
        {
            // 替换后空间超出原空间,则重新分配内存
            sWrkStr.GetBufferSetLength((nSize + nAddSize + nRepSize)<<1);  // x2
        }
        strncpy(sWrkStr + nSize , pOld, nAddSize);
        strncpy(sWrkStr + nSize + nAddSize, strReplace, nRepSize);
        nSize = nSize + nAddSize + nRepSize;
        pOld += nOldSize + nAddSize;        
    }
    if(pNew==NULL)  // 曾经替换过,处理剩余的数据
    {
        * (sWrkStr + nSize) = 0;
        strcat(sWrkStr,pOld);
        strcpy(src,sWrkStr);
    }
    return src;
}