C++ string操作大全及代码演示

 
这篇博客内容包括:
  - string基本操作
  - 查找第n次出现字符“s_tofind”的位置;
  - 按照某一pattern分割字符串;
  - 将string数据转换成double数据.
 
代码如下:

1. string基本操作

//查找第n次出现字符“s_tofind”的位置
int findPos_n(string s, string s_tofind, int n);
//按照某一pattern分割字符串
void cutString(string str, vector<string> &split_str, string pattern);
//将string数据转换成double数据
double stringToDouble(string str);

int main(int argc, char **argv)
{
   
	if(argc != 2)
	{
   
		cout << "Error! Syntax is : ./useString filename" << endl;
		return 1;
	}
	// 读取文件特定行
	string filename = string(argv[1]);
	int line_n = 3;
	string str_line = ReadLine(filename, line_n);//该函数在我的另一篇博客里面
	cout << "the " << line_n << " line is : " << str_line << endl;


	vector<string> split_str;
	cutString("Lab205 is  a big  family !",split_str," ");

	for (int i = 0; i < split_str.size(); ++i)
	{
   
		/* code */
		cout << i << " : " << split_str[i] << endl;
	}

	//define
	string str1 = "Lab205 is a big family ! " ;
	//string str1 = "查找第n次出现字符“s_tofind”的位置";
	string str2 ("I must study SLAM well !");

	//copy string 
	string s_copy1 (str1);  //deep copy
	cout << "s_copy1  is : " << s_copy1 << endl ;//Lab205 is a big family !
	// s_copy1.at(2) = 'c' ;
	 cout << "s_copy1.at(2)  is  :  " << s_copy1.at(2) << endl;//b
	string s_copy2 (str1,7);//from the 7th word to start copy
	cout << "s_copy2 ( str1,7) is :  " << s_copy2 << endl; //is a big family !
	string s_copy3 (str1,3,6);//copy 6 words from the 3th word  on
	cout << "s_copy3 (str1,3,6) is :  " << s_copy3 << endl << endl;
	str1.~string();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值