STL--string基本使用

目录

一.基本概念

string是C++风格的字符串,而string本质上是一个类,其中有许多成员方法。

二.基本使用

1.string的构造

2.string赋值

3.string 字符串拼接

 4.string查找与替换

5.string字符串比较

6.string字符存取

7.string插入和删除

8.string子串


一.基本概念

string是C++风格的字符串,而string本质上是一个类,其中有许多成员方法。

二.基本使用

注意:需要引入头文件  #include<string>

1.string的构造

* `string();`                             //创建一个空的字符串 例如: string str;

 `string(const char* s);`           //使用字符串s初始化

* `string(const string& str);`    //使用一个string对象初始化另一个string对象

* `string(int n, char c);`           //使用n个字符c初始化

int main()
{


	string s1;
	cout << s1 << endl;//输出为空

	const char* s = "chen";
	string s2(s);
	cout << s2 << endl;//chen

	string s3(s2);
	cout << s3 << endl;//chen

	string s4(10, 'c');//使用10个c初始化
	cout << s4 << endl;//cccccccccc
return 0;
}

2.string赋值

* `string& operator=(const char* s);`   //char*类型字符串 赋值给当前的字符串

* `string& operator=(const string &s);` //把字符串s赋给当前的字符串

* `string& assign(const char *s);`       //把字符串s赋给当前的字符串

* `string& assign(const char *s, int n);`//把字符串s的前n个字符赋给当前的字符串

* `string& assign(const string &s);`     //把字符串s赋给当前字符串

* `string& assign(int n, char c);`       //用n个字符c赋给当前字符串

int main()
{
	string str1;
	str1 = "hello world";
	cout << "str1= " << str1 << endl;//str1= hello world

	string str2 = str1;
	cout << str2 << endl;//hello world


  //使用string自带的方法
	string s4;
	s4.assign("hello c++");
	cout << s4 << endl; //hello c++

	s4.assign("hello c++", 5);
	cout << s4 << endl;//hello


	string s5;
	s5.assign(s4);
	cout << s5 << endl;//hello

	string s7;
	s7.assign(6, 'c');
	cout << s7 << endl;// cccccc

	return 0;
}

3.string 字符串拼接

* 实现在字符串末尾拼接字符串

* `string& operator+=(const char* str);`     //重载+=操作符

* `string& operator+=(const char c);`         //重载+=操作符

* `string& operator+=(const string& str);`     //重载+=操作符

* `string& append(const char *s); `           //把字符串s连接到当前字符串结尾

* `string& append(const char *s, int n);`     //把字符串s的前n个字符连接到当前字符串结尾

* `string& append(const string &s);`         //同operator+=(const string& str)

* `string& append(const string &s, int pos, int n);`//字符串s中从pos开始的n个字符连接到字符串结尾

int main()
{   string str1 = "我";

	str1 += "爱玩游戏";

	cout << "str1 = " << str1 << endl; //str1 = 我爱玩游戏
	str1 += ";";
	cout << "str1 = " << str1 << endl;//str1 = 我爱玩游戏;
	
	
	string str2 = "LOL DNF";

	str1 += str2;
	cout << "str1 = " << str1 << endl;//str1 = 我爱玩游戏;LOL DNF


	string str3 = "I";
	str3.append("love");
	str3.append("game abcde", 4);
	cout << "str3=" << str3 << endl;//str3=Ilovegame

   str3.append(str2, 4, 3); // 从下标4位置开始 ,截取3个字符,拼接到字符串末尾
	cout << "str3 = " << str3 << endl;//str3 = IlovegameDNF
}

 4.string查找与替换

* `int find(const string& str) const;` //查找str第一次出现位置

* `int rfind(const string& str) const;`//查找str最后一次位置

* `string& replace(int pos, int n, const string& str); ` //替换从pos开始n个字符为字符串str


int main()
{
    string str11 = "abcdefgde";

    //找到指定字符串在字符串中的最后的位置
	int pos1 = str11.rfind("de");//7
	cout << pos1 << endl;

	string str12 = "abcdefgde";

	str12.replace(1, 3, "1111");
	cout << str12 <<endl; //a1111efgde

}

5.string字符串比较

* `int compare(const string &s) const; `  //与字符串s比较

* `int compare(const char *s) const;`      //与字符串s比较

= 返回   0

\> 返回   1

< 返回  -1

int main()
{  
   //字符串之间的比较
	string s1 = "hello";
	string s2 = "aello";

	int ret = s1.compare(s2);

	if (ret == 0)
		cout << "s1==s2";
	else if (ret > 0)
		cout << "s1>s2" << endl;
	else
		cout << "s1<s2" << endl;//s1>s2
}

6.string字符存取

* `char& operator[](int n); `     //通过[]方式取字符

* `char& at(int n);   `                    //通过at方法获取字符

//strin字符存取
	string str = "hello world";
	cout << str[0] << endl;//h
	cout << str.at(0) << endl;//h

7.string插入和删除

* `string& insert(int pos, const char* s);  `                //插入字符串

* `string& insert(int pos, const string& str); `        //插入字符串

* `string& erase(int pos, int n = npos);`                    //删除从Pos开始的n个字符

string str5 = "hello";
	str5.insert(1, "111");//第一个字符后开始插入
	cout << str5 << endl;//h111ello

	str5.erase(1, 3);//第一个字符开始删除3个字符
	cout << str5 << endl;//hello

8.string子串

* `string substr(int pos = 0, int n = npos) const;`   //返回由pos开始的n个字符组成的字符串

    //获取想要的字符串子串
	string str6 = "abcdefg";
	string substr = str6.substr(1, 3);//裁剪下标1-3的子串
	cout << "substr = " << substr << endl;//substr = bcd


	string email = "hello@sina.com";
	int pos = email.find("@");	
	string username = email.substr(0, pos);
	cout << "subStr = " << username << endl;//subStr = hello


本文章旨在后续查看复习。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值