String 语法

#include<iostream>
using namespace std;
#include"string"
#include"algorithm"

/*初始化*/
int main01()
{   /初始化方法
	string s1 = "aaaaaaaaaa";      //string(const char *s);
	string s2("bbbbbbbbbb");
	string s3(s1);                 //string(const string &t)
	string s4 = s1;                //copy构造函数
	string s5 = s1 + "bbb";        //lianjie
	string s6 = s1 + s2;
	string s0(s1, 0, 3);           //从s1的第0个字符开始取3个字符到s0中
	迭代器初始化
	string s7 = "How are you";
	string s8(s7.begin(), s7.end());
	string s9(s7.begin() + 4, s7.begin() + 7);
	cout << "s1  " << s1 << endl;
	cout << "s2  " << s2 << endl;
	cout << "s3  " << s3 << endl;
	cout << "s4  " << s4 << endl;
	cout << "s5  " << s5 << endl;
	cout << "s6  " << s6 << endl;
	cout << "s7  " << s7 << endl;
	cout << "s8  " << s8 << endl;
	cout << "s9  " << s9 << endl;

	return 0;
}

/*遍历*/
int main02()
{
	string s1 = "Thank you";
/数组法
	
	/*
	operator[]和 at()均返回当前字符串中第 n 个字符, 但二者是有区别的。
主要区别在于 at()在越界时会抛出异常, []在刚好越界时会返回(char)0, 再继续越
界时, 编译器直接出错。 如果你的程序希望可以通过 try,catch 捕获异常, 建议采用 at()。
    */

//const char &operator[] (int n) const;
	for (int i = 0; i < s1.length() ; i++)//int length()求长度
	{
		cout << s1[i];
	}
	cout << endl;


//const char &at(int n) const;
	try
	{
		for (int i = 0; i < s1.length() + 3; i++)
		{
			cout << s1.at(i);
		}
		cout << endl;
	}
	catch (...)
	{
		cout << "发生异常" << endl;
	}
	
//迭代法
	for (string::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it;
	}
	cout << endl;
	return 0;
}

/*string \char * 之间的转化 、 连接*/
int main03()
{
	string s1 = "aaa";
		//1、string---->>char*
		cout << s1.c_str() << endl;//返回一个以'\0'结尾的字符串的首地址
		//2、char*--->>string
		//3、连接
		string s2 = "111";
		string s3 = "222";
		string s4 = s1 + s2;//方法一
		 s2.append(s3);//方法二
		 cout << "s2:" << s2 << endl;
		 cout << "s4:" << s4 << endl;
		
		 return  0;
}
/*拷贝*/
int main04()
{
	/*
	把当前串中以 pos 开始的 n 个字符拷贝到以 s 为起始位置的字符数组中, 返回实际
	拷贝的数目。 注意要保证 s 所指向的空间足够大以容纳当前字符串, 不然会越界。
	*/
	/*
	查找MSDN得知,该成员函数因为可能存在下标越界风险已经被弃用,并提供了
	basic_string::_Copy_s 代替 basic_string::copy
	*/
  int copy(char *s, int n, int pos=0) const;
	string s1 = "ABCDEFG";
	char buff[128] = {0};//先要分配内存
	s1._Copy_s(buff,3,0);//
	cout << "kaobei buff=" << buff << endl;
	return 0;
}

/*查找和替换*/
int main05()
{
	string s1 = "hao hao xue xi hao hao, tian tian xiang shang tian tian.";//55
	find查找语法
	/*
	string s2 = "xi";
	int n1 = s1.find(s2, 0);
	cout << n1 << endl;
	int n = s1.find("tian", 0);//24(tian)
	cout << "返回首次匹配位置find: " << n << endl;//返回首次匹配的开始位置;
	n = s1.find_first_of("111gyz", 0);//38(向g)
	cout << "返回匹配任何字符的第一次位置find_first_of: " << n << endl;//返回值是第一个与指定字符串中任何字符匹配的字符位置;
	n = s1.find_last_of("111gyz", 55);//44 (上g)  最后一次位置是从串尾向前查找
	cout << "返回匹配任何字符的最后一次位置find_first_of: " << n << endl;//返回值是最后一个与指定字符串中任何字符匹配的字符位置;
	n = s1.find_first_not_of("abcdefg", 0);//0(好h)
	cout << "返回不匹配任何字符的第一次位置find_first_of: " << n << endl;
	n = s1.find_last_not_of("abcdefg", 55);//55(天n)最后一次位置是从串尾向前查找
	cout << "返回不匹配任何字符的最后一次位置find_first_of: " << n << endl;
	n = s1.rfind("tian", 55);//51(tian)
	cout << "反向查找:" << n << endl;
	*/

	//求指定字符的出现次数
	/*
	int n = s1.find("xue", 0);
	int times = 0;//tian 出现的次数
	while (n != string::npos)//求tian出现的所有位置,并显示输出
	{
		times++;
		cout << n << endl;//8
		n = n + 1;
		n = s1.find("xue",n);
		
	}
	cout << "times=" << times << endl;;//1
	*/

	/字符替换(将a替换成A)
	
	int n = s1.find("a", 0);
	cout << "替换前:" << s1 << endl;
	while (n != string::npos)
	{
		s1.replace(n,1,"A");//n是a出现的位置,1 指删除一个a字符,改成A
		n++;
		n = s1.find("a",n);
	}
	cout << "替换后:" << s1 << endl;
	return 0;
}

/*插入与删除*/
int main06()
{
	//删除字符;string &erase(int pos=0, int n=npos); //删除 pos 开始的 n 个字符, 返回修改后的字符串
	string s1 = "hello,please enter two world";
	string::iterator it = find(s1.begin(), s1.end(),'l');
	if (it != s1.end())
	{
		s1.erase(it);//迭代器
	}
	cout << s1 << endl;//"helo,please enter two world";
	string s2 = "hello,please enter two world";
	s2.erase(s2.begin() + 5, s2.begin() + 12);//删除“,please”
	cout << s2 << endl;//"hello enter two world"
	s1.erase(s1.begin(),s1.end());//全部删除
	cout << s1.length() << endl;//0
	//插入字符
	/*
	string &insert(int pos, const char *s);
	string &insert(int pos, const string &s);
	//前两个函数在 pos 位置插入字符串 s
	string &insert(int pos, int n, char c); //在 pos 位置 插入 n 个字符 c
	*/
	string s3 = "abcdefg";
	s3.insert(0, "000");
	s3.insert(0, s3);
	cout << "charu zifu hou:" << s3 << endl;//000abcdefg000abcdefg
	return 0;
}

/*字符串比较*/
int main07()
{
	string s1 = "agafnajsasdffdsbg";
	string s2 = "agaahsudifgadfsdb";
	/*运算符
	if (s1 > s2)
		cout << "s1>s2" << endl; 
	if (s1 == s2)
		cout << "s1=s2" << endl;
	if (s1 < s2)
		cout << "s1<s2" << endl;
	*/

	//compare函数
/*
	int compare(const string &s) const; //与字符串 s 比较
int compare(const char *s) const; //与字符串 s 比较
compare 函数在>时返回 1, <时返回 -1, ==时返回 0。 比较区分大小写, 比较时参考字典
顺序, 排越前面的越小。 大写的 A 比小写的 a 小。
*/
	int n=s1.compare(s2);
	if (n == 0)//此处“==”应注意
	{
		cout << "s1=s2" << endl;
	}
	else
	{
		if (n > 0)
			cout << "s1>s2" << endl;
		else
			cout << "s1<s2" << endl;
	}
	return 0;
}

/*常用算法*/
#include"sstream"
int main08()
{
/大小写转换///
	/*
	string s2 = "AAAbbb";
	transform(s2.begin(), s2.end(), s2.begin(), toupper);
	cout << s2 << endl;
	string s3 = "AAAbbb";
	transform(s3.begin(), s3.end(), s3.begin(), tolower);
	cout << s3 << endl;
	*/

/将整数变换成字符串///
/*
    用流类strstream做中间媒介,实现基本数据类型与字符串的相互转化。
    strstring是输入输出流,利用输入输出流功能先填充输出缓冲区,再利用输入流功能依次读缓冲区,
赋值给相应的变量。 
*/
	/*
	int n1 = 12345;
	string s1;
	strstream os1;//理解此语法
	os1 << n1;
	os1 >> s1;
	cout << "将整数变换成字符串:" << s1 << endl;
	*/
/拆分字符串1//
	/*
	string s2 = "How are you?";  //源串
	string s3 = " ";             //按空格拆分
	string sResult;              //拆分结果串
	int n = 0;               //n = s2.find_first_of(s3, 0);//拆分子串结束位置
	int prev= 0;       //拆分子串开始位置
	while ((n = s2.find_first_of(s3, n)) != string::npos)
	{
		sResult = s2.substr(prev, n - prev);//substr函数是一个给定起点和截取长度,截出字符串中一段的函数
		cout << "string=" << sResult << endl;
		prev = ++n;//下一串起始位置、结束位置=当前子串结束位置+1;
		
	}
	if (prev != s2.size())//判断 有无最后一个子串
	{
		sResult = s2.substr(prev, n - prev);
		cout << "string="<<sResult << endl;
	}
	*/
/拆分字符串2//
	string s4 = " ";
	string s5 = "How are you?";
	istringstream istr(s5); //用字符串输入流封装字符串
	while (!istr.eof())//当非字符串输入流末尾
	{
		istr >> s4;  //读输入流便给变量赋值  //getline(istr,s4, ",");
		cout << "string=" << s4 << endl;
	}
	return 0;
}

int main()
{
	main01();//初始化
	main02();//遍历
	main03();//string\char*  连接  
	main04();//拷贝
	main05();//查找
	main06();//插入与删除
	main07();//比较
	main08();//常用算法
	system("pause ");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值