string部分API复习(C++)

版权声明:本博客为博主原创文章,未经博主允许,禁止转载,谢谢合作。 https://blog.csdn.net/weixin_43971252/article/details/88241005

注: 文章为本人为方便复习而写,若有朋友要参考,本文的不清楚的地方可以留言,也可以参考其他网页。

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

//存数据:初始化和赋值API
void test_one()
{
	string str1("hello zhangqu");  //初始化
	cout << str1 << endl;

	//指定方式重写
	str1.assign("youyouyouhh", 6); //将字符串(这里是 "youyouyouhh")的前n(这里是6)个字符赋值给当前字符串(str1)
	cout << str1 << endl;

	string str2;
	str2.assign(str1, 2, 3);  //从字符串str1的第2+1 (因为下标从0开始遍历) 个位置的字符开始拷贝3个字符给当前字符串str2
	cout << str2 << endl;
}

//取出数据
void test_two()
{
	string str("I am from guizhou privince.");
	for (unsigned int i = 0; i < str.size(); i++)
	{
		//cout << str[i] << " ";
		cout << str.at(i) << " ";      //用at取出字符串中位置i对应的字符
	}
	cout << "\n";
	 
	//用at访问字符串会抛出异常而程序不崩溃,用str[]不抛出异常且程序崩溃
	try{
		//cout << str[1000] << endl;
		cout << str.at(1000) << endl;
	}
	catch (...)
	{
		cout << "访问数组越界" << endl;
	}
}

//字符串拼接:尾部追加
void test_three()
{
	//+=
	//不断更新中,先实验以下3种
	/*string& operator+= (const string& str);
	string& operator+= (const char* s);
	string& operator+= (char c);*/

	string str("我");
	string str_two("将要去");

	str += str_two;          //str       
	str += "爱杭州";        //s
	str += '.';           //c
	cout << str << endl;

	//append(不断更新中)
	string str1("hello");

	//append的不同接口
	str1.append("my girlfriend"); //const char *
	str1.append(1, ',');                       //在str1尾部追加n个字符
	str1.append(str);          //const string& 

	//一个汉字算2个字符
	str1.append("一路顺风.", 4);    //尾部追加n个字节的字符
	//cout << str1 << endl;
	string s("i am very strong,np");
	str1.append(s, 2, 7);           //从字符串s的第2+1个位置开始拷贝7个字节到当前字符串str1尾部  
	cout << str1 << endl;
}

//插入与删除
void test_eight() {
	string str1("world");
	string str2("hello ");

	//插入接口1
	str1.insert(0, str2);     //在字符串指定位置(0)插入字符或字符串,若该位置或之后又字符则该位置及其以后的字符将整体后移
	cout << str1 << endl;

	int pos(str1.find("d"));
	str1.insert(pos + 1, " i am very confident");
	cout << str1 << endl;

	//接口2
	string str3("my ");
	str3.insert(3, 6, 'h');      //在指定位置(3)插入n(6)个字符(h)
	cout << str3 << endl;

	//删除
	string str4("hello chain");
	str4.erase(6, 5);
	cout << str4 << endl;
}

//查找
void test_four()
{
	//find:从前往后查找子字符串或字符在当前字符串中第一次出现的位置
	string str("asdfgh");
	int pos( str.find("as") );  //默认从0下标开始查找,返回找到的位置下标
	
	cout << pos << endl;

	//find不同接口
	pos = str.find("abc");
	cout << pos << endl;       //string 查找不到返回-1
	pos = str.find('f');
	cout << pos << endl;
	pos = str.find("fgheijkjk", 0, 3);     //在当前字符串str中查找与给定字符串("fgheijkjk")中前3个字符匹配的第一次出现的位置,从0下标开始查找
	cout << pos << endl;

	//rfind:从后往前查找要匹配字符或字符串在当前字符串中第一次出现的位置,返回值是从前往后计算
	pos = str.rfind("fgh");
	cout << pos << endl;  //3
}

//替换
void test_five()
{
	string str("hello");

	//const char *
	str.replace(2, 3, "world"); //将当前字符串从第2+1个字符开始的3个字符替换成指定字符串("world")
	cout << str << endl;

	string str1("my girlfriend");
	str.replace(0, 1, str1);      //const string&    
	cout << str << endl;
}

//字符串比较大小
//比较过程:从第一个字符开始比较,当比较到两个字符串里的不同字符时结束,比较两个不同字符的编码值确定返回值,与字符串的长度无关,与字符对应的编码数值相关
//返回值:相等为0,大于为1,小于为-1
void test_six() {
	string str1("abcd");
	string str2("abcd");

	if (str1.compare(str2) == 0) {
		cout << "当前字符串与比较的字符串相等" << endl;   //ok
	}
	else if (str1.compare(str2) == 1) {
		cout << "当前字符串大于与其比较的字符串" << endl;
	}
	else  // -1
		cout << "当前字符串小于与其比较的字符串" << endl;
	cout << "\n";

	if(str1.compare("bbcd") == -1)
		cout << "当前字符串小于与其比较的字符串" << endl;   //ok
	else 
		cout << "当前字符串大于与其比较的字符串" << endl;
	cout << "\n";

	//与字符串长度无关
	if(str1.compare("b") == 1)
		cout << "当前字符串大于与其比较的字符串" << endl;
	else if(str1.compare("b") == -1)
		cout << "当前字符串小于与其比较的字符串" << endl;    //ok
	else 
		cout << "当前字符串与比较的字符串相等" << endl;
	cout << "\n";

	if (str1.compare("abcda") == 1)
		cout << "当前字符串大于与其比较的字符串" << endl;
	else if(str1.compare("abcda") == -1)
		cout << "当前字符串小于与其比较的字符串" << endl;    //ok
	else
		cout << "当前字符串与比较的字符串相等" << endl;
	cout << "\n";
}

//获取子串
//用substr来获取当前字符串的子串
//参数:(开始的位置,获取的子串长度)
void test_seven() {
	string s1("asdfghh");

	string sub_of_s1(s1.substr(0, 3));      //获取当前字符串的从0下标开始的长度为3的子串
	cout << "子串: " << sub_of_s1 << endl;
	sub_of_s1 = s1.substr();               //使用默认参数就是拷贝整个字符串
	cout << "子串: " << sub_of_s1 << endl;

	//获取邮箱用户名
	string email("[email protected]");

	int pos = email.find("@");             //返回@在email中第一次出现的位置
	cout << "pos= " << pos << endl;
	string user_name(email.substr(0, pos));
	cout << "用户名: " << user_name << endl;
}

//string与c_style字符串转换
void func(string str) {
	cout << str << endl;
}
void func2(const char *str) {
	cout << str << endl;
}
void c_cpp() {
	string str("abcd");

	//cpp转c
	const char *s = str.c_str();     //把str转为c风格的字符串s
	func(s);                       //const char* 可以隐式类型转换为string

	//c转cpp
	string s_cpp(s);       //把s转换为cpp风格的字符串
	//func2(s_cpp);         //error, string无法隐式类型转换为const char *
}
//参数传递时,c风格可以隐式类型转换为cpp风格

int main()
{
	//test_one();
	//test_two();
	//test_three();
	//test_eight();
	//test_four();
	//test_five();
	//test_six();
	//test_seven();
	c_cpp();
	
	cin.get();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43971252/article/details/88241005