第十二课c++ --- string

string和char 的比较
1) string是一个类, char
是一个指向字符的指针。
string封装了char
,管理这个字符串,是一个char型的容器。
2) string不用考虑内存释放和越界。
string管理char
所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。
3)string提供了一系列的字符串操作函数
查找find,拷贝copy,删除erase,替换replace,插入insert*


1、string的构造函数
1)默认构造函数:
string(); //构造一个空的字符串string s1。
2)拷贝构造函数:
string(const string &str); //构造一个与str一样的string。如string s1(s2)。
3) 带参数的构造函数
string(const char *s); //用字符串s初始化
string(int n,char c); //用n个字符c初始化

2、string的存取字符操作
char &operator[] (int n);
char &at(int n);
operator[]和at()均返回当前字符串中第n个字符,但二者是有区别的。
主要区别在于at()在越界时会抛出异常,[]在刚好越界时会返回(char)0,再继续越界时,编译器直接出错。如果你的程序希望可以通过try,catch捕获异常,建议采用at()。

3、从string取得const char*的操作
const char *c_str() const; //返回一个以’\0’结尾的字符串的首地址

4、把string拷贝到char*指向的内存空间的操作
int copy(char *s, int n, int pos=0) const;
把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目。注意要保证s所指向的空间足够大以容纳当前字符串,不然会越界。

5、string的长度
int length() const; //返回当前字符串的长度。长度不包括字符串结尾的’\0’。
bool empty() const; //当前字符串是否为空

6、string的赋值
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赋给当前字符串
string &assign(const string &s,int start, int n); //把字符串s中从start开始的n个字符赋给当前字符串

7、string字符串连接
string &operator+=(const string &s); //把字符串s连接到当前字符串结尾
string &operator+=(const char *s);//把字符串s连接到当前字符串结尾
string &append(const char *s); //把字符串s连接到当前字符串结尾
string &append(const char *s,int n); //把字符串s的前n个字符连接到当前字符串结尾
string &append(const string &s); //同operator+=()
string &append(const string &s,int pos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾
string &append(int n, char c); //在当前字符串结尾添加n个字符c

8、string的比较
int compare(const string &s) const; //与字符串s比较
int compare(const char *s) const; //与字符串s比较
compare函数在>时返回 1,<时返回 -1,==时返回 0。比较区分大小写,比较时参考字典顺序,排越前面的越小。大写的A比小写的a小。

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

10、string的查找 和 替换
查找
int find(char c,int pos=0) const; //从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos=0) const; //从pos开始查找字符串s在当前字符串的位置
int find(const string &s, int pos=0) const; //从pos开始查找字符串s在当前字符串中的位置
find函数如果查找不到,就返回-1
int rfind(char c, int pos=npos) const; //从pos开始从后向前查找字符c在当前字符串中的位置
int rfind(const char *s, int pos=npos) const;
int rfind(const string &s, int pos=npos) const;
//rfind是反向查找的意思,如果查找不到, 返回-1
替换
string &replace(int pos, int n, const char *s);//删除从pos开始的n个字符,然后在pos处插入串s
string &replace(int pos, int n, const string &s); //删除从pos开始的n个字符,然后在pos处插入串s
void swap(string &s2); //交换当前字符串与s2的值

#include "iostream"
using namespace std;
int main(void)
{
	string s1 = "wbm hello wbm 111 wbm 222 wbm 333";
	cout<<s1.at(0)<< endl;  //at取出对应下标的值
	int index = s1.find("wbm", 0);
	cout << "index: " << index; 
	size_t offindex = s1.find("wbm", 0);
	while (offindex != string::npos){
		cout << "在下标index: " << offindex << "找到wbm\n";
		offindex = offindex + 1;
		offindex = s1.find("wbm", offindex);//查找字符串的位置
	}
	//替换 
	string s2 = "wbm hello wbm 111 wbm 222 wbm 333";
	s2.replace(0, 3, "wbm");
	cout<<s2.c_str()<< endl;
	//求itcast出现的次数
	offindex = s2.find("wbm", 0);
	while (offindex != string::npos)
	{
		cout << "在下标index: " << offindex << "找到wbm\n";
		s2.replace(offindex, 3, "WBM");
		offindex = offindex + 1;
		offindex = s1.find("wbm", offindex);
	}
	cout<< "替换以后的s2:" << s2.c_str() << endl; 
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值