string类详细使用

目录

作用

构造函数

基本赋值操作

获取字符串长度

存取字符操作

拼接操作

查找和替换

比较操作

截取操作

插入与删除

string与char *转换


        C++中string类的详细使用

作用

存储字符的容器(字符串)

注意:

        使用时需要调用头文件 <string>

构造函数

语法:

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

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

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

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

基本赋值操作

语法:

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

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

string& operator=(char c);        //字符赋值给当前的字符串

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个 字符赋值给字符串

获取字符串长度

语法:

int size();

int length();

注意:不包含\0

存取字符操作

语法:

char& operator[](int n);        //通过[]方式取字符,下标越界不会抛出异常

char& at(int n);        //通过at方法获取字符,下标越界会抛出异常

拼接操作

语法:

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

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

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

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

查找和替换

语法:

int find(const string& str, int pos = 0) const;         //查找str第一次出现位置, 从pos开始查找

int find(const char* s, int pos = 0) const;         //查找s第一次出现位置,从pos 开始查找

int find(const char* s, int pos, int n) const;         //从pos位置查找s的前n个字 符第一次位置

int find(const char c, int pos = 0) const;         //查找字符c第一次出现位置

int rfind(const string& str, int pos = npos) const;        //查找str最后一次位 置,从pos开始查找

int rfind(const char* s, int pos = npos) const;        //查找s最后一次出现位置, 从pos开始查找

int rfind(const char* s, int pos, int n) const;        //从pos查找s的前n个字符最 后一次位置

int rfind(const char c, int pos = 0) const;         //查找字符c最后一次出现位置

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

string& replace(int pos, int n, const char* s);         //替换从pos开始的n个字符 为字符串s 注意:查找是不存在返回-1

比较操作

语法:

compare函数在当前对象比参数 > 时返回正数,< 时返回负数,== 时返回0。

比较区分大小写,比较时参考字典顺序,排越前面的越小。大写的A比小写的a小。

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

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

截取操作

语法:

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

插入与删除

语法:

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

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

string& insert(int pos, int n, char c);        //在指定位置插入n个字符c

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

string与char *转换

语法:

//string转char*

string str = "itcast";

const char* cstr = str.c_str();

//char*转string

char* s = "itcast";

string str(s);

  • 22
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
stringC++标准库中的一个重要,用于处理字符串。下面是一些string使用教程: 1. 创建string对象 可以通过以下方式创建string对象: ``` string s1; // 创建一个空字符串 string s2 = "Hello"; // 创建一个字符串并初始化为"Hello" string s3(s2); // 创建一个新的字符串并初始化为s2的值 string s4 = s2 + " World"; // 创建一个新字符串,其值为s2和" World"的连接 ``` 2. 访问string对象的值 可以使用下标运算符[]或at()函数来访问string对象的值。例如: ``` string s = "Hello"; cout << s[0] << endl; // 输出'H' cout << s.at(1) << endl; // 输出'e' ``` 3. 修改string对象的值 可以使用赋值运算符=、+=等运算符来修改string对象的值。例如: ``` string s = "Hello"; s[0] = 'h'; s[1] = 'i'; s += " there"; cout << s << endl; // 输出"hi there" ``` 4. 获取string对象的长度 可以使用size()或length()函数来获取string对象的长度。例如: ``` string s = "Hello"; cout << s.size() << endl; // 输出5 cout << s.length() << endl; // 输出5 ``` 5. 查找子字符串 可以使用find()函数来查找子字符串在string对象中的位置。例如: ``` string s = "Hello world"; size_t pos = s.find("world"); cout << pos << endl; // 输出6 ``` 6. 截取子字符串 可以使用substr()函数来截取string对象的子字符串。例如: ``` string s = "Hello world"; string sub = s.substr(6, 5); // 从第6个位置开始截取5个字符 cout << sub << endl; // 输出"world" ``` 以上是string的一些常见用法。还有很多其他的函数可以用于操作字符串,具体可以参考C++标准库的文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值