• C++ string常用函数用法总结


    目录

    一、构造函数

    二、string的:大小和容量

     三、string的插入:push_back()和insert()

    四、string的比较:compare()

     五、string拼接字符串:append()、+=

    六、string的删除:erase()

    七、string遍历:迭代器或下标

    八、string的字符替换:replace()

    九、string大小写转换:tolower() 和 toupper() 

    十、string数值转换:stoi() 、 atoi() 、 to_string() 和 c_str 

    十一、string的查找:find()

    十二、string的截取:strsub()


    一、构造函数

    string str:生成空字符串

    string s(str):生成字符串为str的复制品

    string s(str, str_begin, str_len):将字符串str中从下标str_begin开始、长度为str_len的部分作为字符串初值

    string s(cstr, char_len):以C_string类型cstr的前char_len个字符串作为字符串s的初值

    string s(num, char):生成num个c字符的字符串

    string s(str, str_index):将字符串str中从下标str_index开始到字符串结束的位置作为字符串初值

    1. string str1; //生成空字符串
    2. string str2("123456789"); //生成"1234456789"的复制品
    3. string str3("12345", 0, 3); //结果为"123"
    4. string str4("012345", 5); //结果为"01234"
    5. string str5(5, '1'); //结果为"11111"
    6. string str6(str2, 2); //结果为"3456789"

    二、string的:大小和容量

    size()和length():返回string对象的字符个数,他们执行效果相同

    max_size():返回string对象最多包含的字符数,超出会抛出异常

    capacity():重新分配内存之前,string对象能包含的最大字符数

    1. string str("1234567");
    2. cout << "size=" << s.size() << endl; // size=7
    3. cout << "length=" << s.length() << endl; // length=7
    4. cout << "max_size=" << s.max_size() << endl; // max_size=4294967294
    5. cout << "capacity=" << s.capacity() << endl; // capacity=15

     三、string的插入:push_back()和insert()

    basic_string& insert (size_type pos, const basic_string& str);
    在原串下标为pos的字符前插入字符串str

    basic_string& insert (size_type pos, const basic_string& str, size_type pos1, size_type n);
    str从下标为pos1开始数的n个字符插在原串下标为pos的字符前

    basic_string& insert (size_type pos, size_type n, char c);
    在原串下标为pos的字符前插入n个字符c
     

    1. // 在s1尾插一个字符
    2. s1.push_back('a');
    3. // insert(pos,char):在制定的位置pos前插入字符char
    4. s1.insert(s1.begin(),'1');
    5. string str="hello";
    6. string s="Hahah";
    7. str.insert(1,s);//在str下标为1的字符e前插入字符串s
    8. string str1="hello";
    9. char c='w';
    10. str1.insert(4,5,c);//在原串下标为4的字符o前插入5个字符c
    11. string str2="hello";
    12. string s2="weakhaha";
    13. str2.insert(0,s2,1,3);//将字符串s2从下标为1的e开始数3个字符,分别是eak,插入原串的下标为0的字符h前

    四、string的比较:compare()

    bool operator==(const string &s1,const string &s2) const;        //比较两个字符串是否相等

    运算符">","<",">=","<=","!="均被重载用于字符串的比较;

     用于比较字符串。compare 成员函数有以下返回值:

    • -1 表示当前的字符串小;
    •  0 表示两个字符串相等;
    • 1 表示另一个字符串小。

    compare()比较时逐字符比较的,一旦能比较出结果,就不再比较了。

    1. string s1="abandon";
    2. string s2="about";
    3. int b=s1.compare(s2);//直接比较,s1小于s2,故返回-1
    4. cout<
    5. int c=s1.compare(2,4,s2);//s1下标为2的字符a开始的4个字符ando和s2进行比较。ando大于s2故返回1
    6. cout<
    7. int d=s1.compare(2,4,s2,1,3);
    8. cout<//s1下标为2的字符a开始的4个字符ando和s2下标为1的字符b开始的3个字符bou比较。前者小,故返回-1。
    9. string s3="abc";
    10. string s4="abc";
    11. int e=s3.compare(s4);//相等返回0
    12. cout<

     五、string拼接字符串:append()、+=

    1. //方法一:append()
    2. string s1("abc");
    3. s1.append("def");
    4. cout<<"s1:"<// s1:abcdef
    5. // 方法二:+= 操作符
    6. string s2 = "abc";
    7. //s2 += "def";
    8. string s3 = "def";
    9. s2 += s3.c_str();
    10. cout<<"s2:"<// s2:abcdef

    六、string的删除:erase()

    iterator erase(iterator p):删除字符串中p所指的字符

    iterator erase(iterator first, iterator last):删除字符串中迭代器区间 [first, last) 上所有字符

    string& erase(size_t pos, size_t len):删除字符串中从索引位置 pos 开始的 len 个字符

    void clear():删除字符串中所有字符

    1. string str = "123456789";
    2. str.erase(3, 3);
    3. cout<// 123789
    4. str.erase(str.begin() + 3);
    5. cout<// 12356789
    6. str.erase(str.begin() + 3, str.begin() + 6);
    7. cout<// 123789


    七、string遍历:迭代器或下标
     

    1. string s1("abcdef");
    2. // 正向迭代器
    3. string::iterator iter = s1.begin();
    4. for( ; iter < s1.end() ; iter++)
    5. {
    6. cout<<*iter;
    7. }
    8. cout<//abcdef
    9. // 反向迭代器
    10. string::reverse_iterator riter = s1.rbegin();
    11. for( ; riter < s1.rend() ; riter++)
    12. {
    13. cout<<*riter;
    14. }
    15. cout<//fedcba

    八、string的字符替换:replace()

    string& replace(size_t pos, size_t n, const char *s):将当前字符串从pos索引开始的n个字符,替换成字符串s

    string& replace(size_t pos, size_t n1, size_t n2, char c):将当前字符串从pos索引开始的n1个字符,替换成n2个字符c

    string& replace(iterator i1, iterator i2, const char* s):将当前字符串[i1, i2)区间中的字符串替换为字符串s

    1. string s1("hello,world!");
    2. s1.replace(6, 5, "girl"); // 结果:hello,girl.
    3. s1.replace(s1.size() - 1, 1, 1, '.'); // 结果:hello,world.
    4. s1.replace(s1.begin(), s1.begin() + 5, "boy"); // 结果:boy,girl.

    九、string大小写转换:tolower() 和 toupper() 

    tolower(char)  、 toupper(char) :将字符进行大小写转换

    1. string s = "ABCDEFG";
    2. for( int i = 0; i < s.size(); i++ )
    3. {
    4. s[i] = tolower(s[i]);
    5. }
    6. cout << s << endl; //abcdefg
    7. transform(s.begin(), s.end(), s.begin(), ::toupper);
    8. cout << s << endl; //"ABCDEFG"

    十、string数值转换:stoi() 、 atoi() 、 to_string() 和 c_str 

    to_string() 函数 把数值类型如int、double、long等转化为string

    stoi() 的形参是const string*,而atoi() 的形参是const char*

    c_str() 能够将 string 字符串转换为C风格的字符串,并返回该字符串的 const 指针(const char*)-简而言之:将const string*转化为const char*

    1. string s1("1234567");
    2. char* s2 = "1234567";
    3. int a = stoi(s1);
    4. int b = atoi(s2);
    5. int c = atoi(s1.c_str());
    6. cout << a << endl;
    7. cout << b << endl;
    8. cout << c << endl;
    1. int a = 4;
    2. double b = 3.14;
    3. string str1, str2;
    4. str1 = to_string(a);
    5. str2 = to_string(b);
    6. cout << str1 << endl;
    7. cout << str2 << endl;

    虽然 C++ 提供了 string 类来替代C语言中的字符串,但是在实际编程中,有时候必须要使用C风格的字符串(例如打开文件时的路径)

    1. string path = "D:\\demo.txt";
    2. FILE *fp = fopen(path.c_str(), "rt");

    十一、string的查找:find()

    1. /*
    2. string的find()函数用于找出字母在字符串中的位置。
    3. find(str,position)
    4. find()的两个参数:
    5. str:是要找的元素
    6. position:字符串中的某个位置,表示从从这个位置开始的字符串中找指定元素。
    7. 可以不填第二个参数,默认从字符串的开头进行查找。
    8. 返回值为目标字符的位置,当没有找到目标字符时返回npos。
    9. */
    10. string s = "hello world!";
    11. cout << s.find("e") << endl; // 1
    12. string s = "hello world!";
    13. if (s.find("a") == s.npos) {
    14. cout << "404 not found" << endl;
    15. }
    16. string s = "hello world!";
    17. cout << s.find("l",5) << endl; //9
    18. //找到目标字符在字符串中第一次出现和最后一次出现的位置
    19. string s = "hello world!";
    20. cout << "first time occur in s:"<find_first_of("l") << endl; // 2
    21. cout << "last time occur in s:" << s.find_last_of("l") << endl; // 9
    22. //反向查找
    23. string s = "hello world!";
    24. cout << s.rfind("l") << endl;

    十二、string的截取:strsub()

    string substr (size_t pos = 0, size_t len = npos) const;

    pos 为要提取的子字符串的起始下标,len 为要提取的子字符串的长度。

    返回一个string,包含从pos开始的n个字符的拷贝

    pos默认值是0,len的默认值是size() - pos,即不加参数会默认拷贝整个string

    1. #include
    2. #include
    3. using namespace std;
    4. int main(){
    5. string s1 = "first second third";
    6. string s2;
    7. s2 = s1.substr(6, 6);
    8. cout<< s1 <
    9. cout<< s2 <
    10. return 0;
    11. }

    pos的值超过string的大小,则substr函数会抛出一个out_of_range异常

    pos + len的值超过了string的大小,则substr会调整n的值,只拷贝到string的末尾


    那么本次文章到此结束,有帮助的话,点赞支持一下吧

    谢谢大噶! 

  • 相关阅读:
    MyBatis之分页查询:MyBatis PageHelper
    Fundebug JavaScript插件支持监控HTTP请求数据
    Elasticsearch查询过程
    网络安全人才缺口超百万,如今的就业情况怎样?
    Mac用NTFS文件夹读写NTFS硬盘 NTFS能复制多大的文件
    UVA 294 约数 Divisors
    【已解决】将一个2708行64列的在GPU上的张量z0矩阵保存下来,格式为csv
    uniapp用vue3.0组合式API开发微信小程序子组件内使用uni-ui扩展组件无法修改样式?
    KEIL5.39 5.40 fromelf 不能生成HEX bug
    【JS】Chapter14-深入面向对象
  • 原文地址:https://blog.csdn.net/qq_61514490/article/details/126165076