CString 中 Replace、Left、Mid、SpanExcluding、GetLength 的用法

注意:本章内容应用示例采用的字符集为:多字节字符集
1.Replace( )函数:

#include<iostream>
#include<atlstr.h>
using namespace std;

int main()
{
	CString str = _T("ok,mrjk");
	int count = str.Replace(_T("ok"), _T("oklkw"));
	cout << count << endl; 
	cout << str << endl;
	getchar();
    return 0;
}

运行结果如下:

2.Left( )函数:

#include<iostream>
#include<atlstr.h>
using namespace std;

//字符串的截取
int main()
{
	CString m_x;
	CString m_ID = _T("00112233");
	m_x = m_ID.Left(3);

	cout << m_x << endl;

	getchar();
	return 0;
}

运行结果如下:

3.Mid( )函数:

#include<iostream>
#include<atlstr.h>
using namespace std;

int main()
{
	CString m = _T("ABCDEF");
	m = m.Mid(2, 3);

	cout << m << endl;

	getchar();
    return 0;
}

运行结果如下:

4.SpanExcluding( )函数:

#include<iostream>
#include<atlstr.h>
using namespace std;

int main()
{
	CString str = L"ABWCYYCDEF";
	str = str.SpanExcluding(_T("CDE"));

	cout << str << endl;  //ABW

	getchar();
	return 0;
}

运行结果如下:

#include<iostream>
#include<atlstr.h>
using namespace std;

int main()
{
	CString str = _T("ABWCYYCDEF");
	str = str.SpanExcluding(_T(""));

	cout << str << endl;   //将字符串全部输出

	getchar();
    return 0;
}

运行结果如下:

4.GetLength ( )函数:

#include<iostream>
#include<atlstr.h>
using namespace std;

int main()
{
	CString sText = _T("string");
	CString RDate = sText.SpanExcluding(_T(" "));
	cout << "RDate: " << RDate << endl;
	cout << "RDate.GetLength(): " << RDate.GetLength() << endl;  // 6

	cout <<"第一个: "<< sText << endl;
	cout << "sText.GetLength(): " << sText.GetLength() << endl;  // 6

	CString RID = sText.SpanExcluding(_T(""));
	cout << "第二个: " << RID << endl;

	sText = sText.Mid(RID.GetLength() + 1, sText.GetLength());

	cout << "测试1: " << sText << endl;
	cout << "sText.GetLength()测试1: " << sText.GetLength() << endl;

	CString RData = sText.SpanExcluding(_T("\r\n"));
	cout << "测试2: " << RData << endl;
	
	getchar();
	return 0;
}

运行结果如下:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值