C++中String操作

C++字符串操作实例

程序1

#include<string>
#include<iostream>

using namespace std;

int main()
{
	string str("songjiehahahh");

	string::size_type pos=str.find("jie");

	if(pos==string::npos)
		cout<<"Not fount jie in string str"<<endl;
	else
		cout<<"jie is found in position "<<pos<<" of str"<<endl;
	return 0;
}

程序2

#include<string>
#include<iostream>

using namespace std;

int main()
{
	string numberic("0123456789");
	
	string test("song12asdfd43tfg");

	string::size_type pos=0;
	while((pos=test.find_first_of(numberic,pos))!=string::npos)
	{
		cout<<"found numberic in test at index "<<pos<<" :"<<test[pos]<<endl;
		pos++;
	}

	return 0;
}

程序3

//该程序完成将字符串中的单词简单地匹配出来

#include<string>
#include<iostream>
#include<vector>

using namespace std;

int main()
{
	string space(" ");
	
	string test("I love you.");

	string::size_type pos=0;
	string::size_type pre_pos=0;

	vector<string> svec;

	while((pos=test.find_first_of(space,pos))!=string::npos)
	{
		//cout<<test.substr(pre_pos,pos-pre_pos)<<endl;
		svec.push_back(test.substr(pre_pos,pos-pre_pos));
		pre_pos=++pos;
	}
	//cout<<test.substr(pre_pos)<<endl;;
	svec.push_back(test.substr(pre_pos,pos-pre_pos));

	vector<string>::const_iterator iter=svec.begin();
	vector<string>::const_iterator iter_end=svec.end();

	for(;iter!=iter_end;iter++)
	{
		cout<<*iter<<endl;
	}

	return 0;
}


转载于:https://my.oschina.net/u/1579376/blog/261600

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值