实例013——实现查找、替换字符串

一、目录:

二、程序主要源代码:

// 字符串替换
void CSearchReplaceStrDlg::OnReplace() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	if(m_strFind.IsEmpty())
	{
		AfxMessageBox("查找的字符串为空!");//判断编辑框字符串是否为空
		return;
	}
	int num;
	num=m_strData.Replace(m_strFind,m_strReplace);//替换字符串
	CString str;
	str.Format("共完成了%d处替换",num);
	AfxMessageBox(str);
	UpdateData(false);
}

//字符串查找
void CSearchReplaceStrDlg::OnSearch() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	if(m_strFind.IsEmpty())//判断编辑框字符串是否为空
	{
		AfxMessageBox("查找的字符串为空!");
		return;
	}
	int pos=0;//记录查找位置
	CString strpos,temp;
	strpos.Format("%s字符串在原字符串中的起始位置为:\n",m_strFind);
	while((pos>=0)&&(pos<m_strData.GetLength()))
	{
		pos=m_strData.Find(m_strFind,pos);//查找字符串
		if(pos>=0)
		{
			temp.Format("%d ",pos);
			strpos+=temp;
			pos+=m_strFind.GetLength();//跳出循环,累计已经查找得字符长度
			CString pos_x;
			pos_x.Format("%d",pos);
		}
	}
	AfxMessageBox(strpos);
}

 三、程序运行结果:

四、资源:

 https://download.csdn.net/download/sunjikui1255326447/12263176

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值