CString的常见使用方式例子代码

这段代码展示了如何使用CString的Split方法以逗号为分隔符切割字符串,并将结果存储到vector中。接着,通过swscanf_s函数解析输入的四个参数,并进行错误检查。最后,利用这些参数生成新的字符串并更新到视图。
摘要由CSDN通过智能技术生成
vector<CString> Split(CString string)
{
	CString oriStr=string;
	vector<CString> strVec;
	
	while (true)
	{
		CString n = oriStr.SpanExcluding(L",");//逗号分隔字符串
		strVec.push_back(n);
		oriStr = oriStr.Right(oriStr.GetLength()-n.GetLength()-1);
		
		if (oriStr.IsEmpty())
		{
			break;
		}
	}
	return strVec;
}


void CgenerateTxtDlg::OnBnClickedButton1()
{
	//CString m_input,m_output对应视图层控件
	std::vector<CString> list = Split(m_input);
	
	if(list.size()!=4)
	{
		AfxMessageBox(L"输入参数不合法");
		return;
	}

	swscanf_s(list[0].GetString(),L"%d",&row);
	swscanf_s(list[1].GetString(),L"%d",&col);
	swscanf_s(list[2].GetString(),L"%d",&width);
	swscanf_s(list[3].GetString(),L"%d",&height);

	TRACE("row,col,width,height=%d,%d,%d,%d\n",row,col,width,height);

	int count =0;
	CString csDst;
	for (int i = 0; i < row; i++)
	{
		for(int j = 0; j <col; j++)
		{
			CString cs;			
			cs.Format(L"a%02d = %d %d %d %d\r\n",count,j*width,i*height,width,height);

			csDst.Append(cs);

			count++;
		}
	}

	m_output = csDst;

	UpdateData(FALSE);//数据上传到视图
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
`CString`类提供了`Find`系列函数来查找字符串中指定子串的位置,可以帮助我们进行字符串的处理。下面是一些常用的`Find`函数: 1. `int Find(LPCTSTR lpszSub)`:在当前字符串中查找子串`lpszSub`,返回子串在当前字符串中首次出现的位置,如果找不到则返回-1。 示例代码: ```cpp CString str = "hello world"; int pos = str.Find("world"); if (pos != -1) { cout << "子串在字符串中的位置:" << pos << endl; } else { cout << "未找到子串" << endl; } ``` 输出为:`子串在字符串中的位置:6`。 2. `int Find(TCHAR ch)`:在当前字符串中查找字符`ch`,返回字符在当前字符串中首次出现的位置,如果找不到则返回-1。 示例代码: ```cpp CString str = "hello world"; int pos = str.Find('w'); if (pos != -1) { cout << "字符在字符串中的位置:" << pos << endl; } else { cout << "未找到字符" << endl; } ``` 输出为:`字符在字符串中的位置:6`。 3. `int ReverseFind(LPCTSTR lpszSub)`:在当前字符串中查找子串`lpszSub`,返回子串在当前字符串中最后一次出现的位置,如果找不到则返回-1。 示例代码: ```cpp CString str = "hello world"; int pos = str.ReverseFind('l'); if (pos != -1) { cout << "字符在字符串中的位置:" << pos << endl; } else { cout << "未找到字符" << endl; } ``` 输出为:`字符在字符串中的位置:9`。 4. `int FindOneOf(LPCTSTR lpszCharSet)`:在当前字符串中查找字符集`lpszCharSet`中的任何一个字符,返回第一个匹配字符在当前字符串中的位置,如果找不到则返回-1。 示例代码: ```cpp CString str = "hello world"; int pos = str.FindOneOf("aeiou"); if (pos != -1) { cout << "匹配字符在字符串中的位置:" << pos << endl; } else { cout << "未找到匹配字符" << endl; } ``` 输出为:`匹配字符在字符串中的位置:1`。 除了上面的这些函数,`CString`类还提供了其他的`Find`系列函数,可以根据需要选择使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值