C++string类常用方法

系统管理员 2022-05-14 03:19 368阅读 0赞
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int main()
  5. {
  6. string str1 = "hello";
  7. string* str2 = new string("hello");
  8. string str3 = "world";
  9. //获取字符串长度
  10. int length = str1.length();
  11. cout << "调用str.length()函数获取字符串长度:" << length << endl;
  12. length = str1.size();
  13. cout << "调用str.size()函数获取字符串长度:" << length << endl;
  14. //字符串连接
  15. string str4 = str1 + str3;
  16. cout << "字符串连接结果:" << str4 << endl;
  17. //字符串比较
  18. if (str1 < str3)
  19. cout << "字符串比较:" << "str1<str2" << endl;
  20. //获取字符串的第一个字符
  21. string::const_iterator it = str1.begin();
  22. cout << *it << endl;
  23. //获取字符串的最后一个字符
  24. //end是指向最后一个字符后面的元素,而且不能输出,所以cout << *it << endl;这样输出会报错
  25. it = str1.end();
  26. it--;
  27. cout << *it << endl;
  28. //倒置串
  29. reverse(str1.begin(), str1.end());
  30. cout << "倒置串:" << str1 << endl;
  31. //字符串转字符数组
  32. //不推荐的用法,但是需要了解
  33. string a = "abc123";
  34. //这里必须为const char *,不能用char *,不然下一句会报错
  35. const char *b;
  36. b = a.c_str();
  37. cout << "a:" << a << endl;
  38. cout << "b:" << b << endl;
  39. a = "asd456";
  40. cout << "a:" << a << endl;
  41. cout << "b:" << b << endl;
  42. //推荐用法
  43. string c = "abc123";
  44. char *d = new char[20];
  45. //因为这里没有直接赋值,所以指针类型可以不用const char *
  46. strcpy(d, c.c_str());
  47. cout << "c:" << c << endl;
  48. cout << "d:" << d << endl;
  49. c = "asd456";
  50. cout << "c:" << c << endl;
  51. cout << "d:" << d << endl;
  52. //查找串
  53. //find-从指定位置起向后查找,直到串尾
  54. string st1("babbabab");
  55. //1,默认从位置0(即第1个字符)开始查找
  56. cout << st1.find('a') << endl;
  57. //在st1中,从位置2(b,包括位置2)开始,查找a,返回首次匹配的位置
  58. cout << st1.find('a', 2) << endl;//4
  59. //两句均输出1,原因是计算机中-1和4294967295都表示为32个1(二进制)
  60. cout << (st1.find('c', 0) == -1) << endl;//1
  61. cout << (st1.find('c', 0) == 4294967295) << endl;//1
  62. string st2("aabcbcabcbabcc");
  63. str1 = "abc";
  64. //从st2的位置2(b)开始匹配,返回第一次成功匹配时匹配的串(abc)的首字符在st2中的位置,
  65. //失败返回-1
  66. cout << st2.find(str1, 2) << endl;//6
  67. //取abcdefg得前3个字符(abc)参与匹配,相当于st2.find("abc", 2)
  68. cout << st2.find("abcdefg", 2, 3) << endl;//6
  69. //rfind-从指定位置起向前查找,直到串首
  70. cout << st1.rfind('a', 7) << endl;//6
  71. //find_first_of-在源串中从位置pos起往后查找,只要在源串中遇到一个字符,
  72. //该字符与目标串中任意一个字符相同,就停止查找,返回该字符在源串中的位置;若匹配失败,返回-1
  73. string str6("bcgjhikl");
  74. string str7("kghlj");
  75. //从str1的第0个字符b开始找,g与str2中的g匹配,停止查找,返回g在str1中的位置2
  76. cout << str6.find_first_of(str7, 0) << endl;//2
  77. //find_last_of-与find_first_of函数相似,只不过查找顺序是从指定位置向前
  78. string str("abcdecg");
  79. //从str的位置6(g)开始向前找,g不匹配,再找c,c匹配,停止查找,返回c在str中的位置5
  80. cout << str.find_last_of("hjlywkcipn", 6) << endl;//5
  81. //find_first_not_of-在源串中从位置pos开始往后查找,只要在源串遇到一个字符,
  82. //与目标串中的任意字符都不相同,就停止查找,返回该字符在源串中的位置;
  83. //若遍历完整个源串,都找不到满足条件的字符,则返回-1
  84. //从源串str的位置0(a)开始查找,目标串中有a,匹配,..,找d,目标串中没有d(不匹配),
  85. //停止查找,返回d在str中的位置3
  86. cout << str.find_first_not_of("kiajbvehfgmlc", 0) << endl;//3
  87. //find_last_not_of-与find_first_not_of相似,只不过查找顺序是从指定位置向前
  88. cout << str.find_last_not_of("kiajbvehfgmlc", 6) << endl;//3
  89. system("pause");
  90. return 0;
  91. }

70

发表评论

表情:
评论列表 (有 0 条评论,368人围观)

还没有评论,来说两句吧...

相关阅读

    相关 自定义CString

    问题描述: 自定义一个CString类,包括获取字符串长度,字符串查找,字符串比较,字符串取子串等都自己来实现。 参考代码: MyString.h文件 prag