请问大家C++里面有没有字符串替换函数? 类似于VB里面的Stringreplace如果没有 应该怎么样写一个?不能用MFC的cstring类,大家帮帮忙 100分送了
解决方案 »
- 定时器的问题,定时器要在何时创建,如何调用?
- 递归查找的小问题,速度来解决送分,附代码~~~~~~~~~
- 求组合算法:1,2,3,4,5,6, 这6个数分成3组,每2个数1组,输出全部组合。
- 关于数的存储结构应用的问题和大家交流下
- HOOK CreateProcessA/W 结果在打开一些程序时系统崩溃了
- send函数老是返回10014
- 想交一些VC的朋友,大家不防公开一下QQ和e-mail
- 初学者:VC函数void GetLBText(int nIndex,CString& rstring) const;中CString& rstring表示什么意
- 代码从VC6转vs2008后字符出现很多乱码
- 不安装WinpCap能否发送ARP欺骗包?
- 用VC如何作一个类似frontpage那样的网页编辑器 在线等待 急急急
- vc 不要买 北京希望出版社的书
{
// can't have empty or NULL lpszOld int nSourceLen = SafeStrlen(lpszOld);
if (nSourceLen == 0)
return 0;
int nReplacementLen = SafeStrlen(lpszNew); // loop once to figure out the size of the result string
int nCount = 0;
LPTSTR lpszStart = m_pchData;
LPTSTR lpszEnd = m_pchData + GetData()->nDataLength;
LPTSTR lpszTarget;
while (lpszStart < lpszEnd)
{
while ((lpszTarget = _tcsstr(lpszStart, lpszOld)) != NULL)
{
nCount++;
lpszStart = lpszTarget + nSourceLen;
}
lpszStart += lstrlen(lpszStart) + 1;
} // if any changes were made, make them
if (nCount > 0)
{
CopyBeforeWrite(); // if the buffer is too small, just
// allocate a new buffer (slow but sure)
int nOldLength = GetData()->nDataLength;
int nNewLength = nOldLength + (nReplacementLen-nSourceLen)*nCount;
if (GetData()->nAllocLength < nNewLength || GetData()->nRefs > 1)
{
CStringData* pOldData = GetData();
LPTSTR pstr = m_pchData;
AllocBuffer(nNewLength);
memcpy(m_pchData, pstr, pOldData->nDataLength*sizeof(TCHAR));
CString::Release(pOldData);
}
// else, we just do it in-place
lpszStart = m_pchData;
lpszEnd = m_pchData + GetData()->nDataLength; // loop again to actually do the work
while (lpszStart < lpszEnd)
{
while ( (lpszTarget = _tcsstr(lpszStart, lpszOld)) != NULL)
{
int nBalance = nOldLength - (lpszTarget - m_pchData + nSourceLen);
memmove(lpszTarget + nReplacementLen, lpszTarget + nSourceLen,
nBalance * sizeof(TCHAR));
memcpy(lpszTarget, lpszNew, nReplacementLen*sizeof(TCHAR));
lpszStart = lpszTarget + nReplacementLen;
lpszStart[nBalance] = '\0';
nOldLength += (nReplacementLen - nSourceLen);
}
lpszStart += lstrlen(lpszStart) + 1;
}
ASSERT(m_pchData[nNewLength] == '\0');
GetData()->nDataLength = nNewLength;
} return nCount;
}
但是STL的库里我记得好象有函数
好久不用了 要去查查
总之针对于char型数组,可以按地址、下标逐一判断操作
string的话,不用我多说啦