C++字符串常见操作函数

常用函数如下:

#include<iostream>
#include<string>
#include<iterator>
#include<algorithm>
#include<vector>
using namespace std;

int main()
{
	/* 1 插入函数
	 * push_back()、insert()
	 */ 
	{
		string ss = "Jaminliu";
		ss.push_back('p');					// 尾部插入一个字符
		ss.insert(ss.begin(), 'P');			// 在字符串头部插入
		ss.insert(ss.end(), 'P');			// 在字符串尾部插入
		cout << "插入函数  ss is: " << ss << endl;
	}

	/* 2 遍历字符串
	 * 正向:string::iterator it=s.begin();it!=s.end();it++;
	 * 反向:string::reverse_iterator it=s.rbegin();it!=s.rend();it++;
	 */
	{
		string ss = "Jaminliu";
		for (string::iterator it = ss.begin(); it != ss.end(); ++it)
		{
			cout << *it << " ";
		}
		cout << endl;

		for (string::reverse_iterator it = ss.rbegin(); it != ss.rend(); ++it)
		{
			cout << *it << " ";
		}
		cout << endl;
	}

	/* 3 查找函数
	 * 查找一个字符串:s.find(ss),找到返回ss在s中的起始位置,否则返回-1;
	 * 从某一个位置开始查找某个字符:s.find(‘t’,6),从s的位置6开始查找字符’t’,找到则返回位置,否则返回-1;
	 * 从末尾开始查找某个字符:s.rfind(‘t’);
	 * 从某个位置开始查找第一个不属于s1的字符位置,例如s=“cdcdscds”,s1=“cdcdfff”,s.find_first_not_of(s1)=4,s的位置4字符不属于s1;
	 */
	{
		string s = "lkjljJaminliujlkjl";
		string ss = "Jaminliu";
		cout << "查找函数  s.find(ss) is: " << s.find(ss) << endl;
		cout << "查找函数  s.find('J', 3) is: " << s.find('J', 3) << endl;
		cout << "查找函数  s.rfind('j') is: " << s.rfind('j') << endl;
		cout << "查找函数  s.find_first_not_of(ss) is: " << s.find_first_not_of(ss) << endl;
	}

	/* 4 排序函数
	 * sort(s.bgein(),s.end())
	 */
	{
		vector<string> sArray = {"jaminliu", "jamin", "liu", "HelloWorld"};
		sort(sArray.begin(), sArray.end());
		for (auto it : sArray)
		{
			cout << it << " ";
		}
		cout << endl;
	}

	/* 5 分割截取函数
	 * substr()、strtok()
	 */
	{
		// 1 substr()
		string ss = "Jaminliu";
		string s_substr = ss.substr(2, 5); // 从2位置开始的5个字符组成的子串
		cout << "分割截取函数 s_substr is:  " << s_substr << endl;
	}

	/* 6 删除函数
     * erase()
	 */
	{
		string ss = "Jaminliu";
		ss.erase(ss.begin() + 3);

		cout << "删除函数 ss is:  " << ss << endl;
	}

	/* 7 替换函数
	 * repalce()
	 * s.replace(pos,len,ss),将s从pos开始的len个字符替换成ss
	 * s.replace(pos,n1,n2,c),将s从pos开始的n1个字符替换成n2个字符c
	 */

	 /* 8 比较函数
	  * compare()
	  * s.compare(ss)=0说明ss == s,返回1表明s>ss,返回-1表明s<ss
	  */

	system("pause");
	return 0;
}

字符串分割各种方式总结如下:

1)模仿boost库中split函数实现字符分割

void SplitFun(const string& str, vector<string>& vec_str, const string& delimiter)
{
	// 找到第一个不是分隔符的位置
	string::size_type lastPos = str.find_first_not_of(delimiter, 0);
	// 以lastPos为起点,找到第一个分隔符所在的位置
	string::size_type pos = str.find_first_of(delimiter, lastPos);

	while (string::npos != pos || string::npos != lastPos)
	{	// 将lastPos为起点,取pos-lastPos个字符,push到vector中
		vec_str.push_back(str.substr(lastPos, pos - lastPos));
		// 继续以pos为起点,知道不是分隔符的位置
		lastPos = str.find_first_not_of(delimiter, pos);
		// 继续以lastPos为起点,找到第一个分隔符所在的位置
		pos = str.find_first_of(delimiter, lastPos);
	}
}
int main(int argc, char *argv[])
{
	string str("600381.sh,000001.sz,600001.sh,,,000002.sz");
	vector<string>vec_str;
	SplitFun(str, vec_str, ",");
	for (int i = 0; i < vec_str.size(); i++)
	{
		cout << vec_str[i] << endl;
	}

	system("pause");
	return 0;
}

说明:后续将逐步补充其他字符串分割demo

参考链接:

(26条消息) 分割字符串的比较完美实现(c++,stl)_wyx819的专栏-CSDN博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值