- 字符串的使用场景?
- C 风格字符串 (char*) 的定义,及常用函数使用
- C++ 标准库字符串 (std::string) 及常用函数使用
字符串类型可以用来表示各种名称、地址、描述、备注等文本数据;主要有 C 风格字符串(char*)和 C++ 风格字符串(std::string、std::wstring)二种字符串;
例子如下:
- #include
-
- // 例子1
- char s1[260]{ "abcd" };
- std::cout << "s1:" << s1 << std::endl;
- // 例子2
- char s2[260]{""};
- strcpy(s2, "abcd");
- std::cout << "s2:" << s2 << std::endl;
- // 例子3
- const char* s3 = "abcd";
- std::cout << "s3:" << s3 << std::endl;
- // 例子4
- char* s4 = new char[260];
- strcpy(s4, "abcd");
- std::cout << "s4:" << s4 << std::endl;
- delete[] s4;
- #include
-
- char s1[260]{ "1111" };
- char s2[260]{ "22222222222211113333aaaa456" };
- // 复制字符串
- strcpy(s1, "aaaa");
- // 计算字符长度
- size_t len1 = strlen(s1);
- std::cout << "s1:" << s1 << ", strlen:" << len1 << std::endl;
- // 比较字符串:(s1
s2)=1 - const int cmp = strcmp(s1, s2);
- std::cout << s1 << " strcmp " << s2 << " = " << cmp<< std::endl;
- // 查找字符
- const char* s3 = strchr(s2, '1'); // 查找字符,找不到返回nullptr
- const int findIndex1 = (nullptr == s3) ? -1 : (s3 - s2); // 计算查找字符排序;找不到-1
- std::cout << s2 << " strchr " << '1' << " = " << findIndex1 << std::endl;
- // 查找子字符串
- const char* s4 = strstr(s2, s1); // 查找子字符串,找不到返回nullptr
- const int findIndex2 = (nullptr == s4) ? -1 : (s4 - s2); // 计算查找子字符串排序;找不到-1
- std::cout << s2 << " strstr " << s1 << " = " << findIndex2 << std::endl;
- // 字符串拼接
- char stemp[260];
- strcpy(stemp, s1);
- const char* s5 = strcat(s1, s2); // 把s2拼接到s1的后面,同时把字符串指针返回s4;(s1==s4)
- std::cout << stemp << " strstr " << s2 << " = " << s5 << std::endl;
- // 格式化字符串
- char s6[260];
- int i = 100;
- float f = 200.123;
- const char* str = "Hello!";
- sprintf(s6, "i=%i, f1=%f, f2=%.2f s=%s", i, f, f, str);
- std::cout << "sprintf:" << s6 << std::endl;
初始化字符串
- #include
-
- // std::string初始化
- std::string s1("1111"); // 直接给定内容
- std::string s2 = "abcd"; // 直接给定内容
- std::string s3(3, 'b'); // 给定3个'b'='bbb'
- std::string s4(s3, 2); // 从第2个取到最后='11abcd'
- std::string s5(s3, 2, 4); // 从第2个取4个=11ab
- std::cout << "s1:" << s1 << std::endl;
- std::cout << "s2:" << s2 << std::endl;
- std::cout << "s3:" << s3 << std::endl;
- std::cout << "s4:" << s4 << std::endl;
- std::cout << "s5:" << s5 << std::endl;
重载+, +=, =
- #include
-
- const std::string s1("1111");
- const std::string s2("2222");
- std::string s3 = s1 + s2; // 11112222
- std::cout << s1 << " + " << s2 << " = " << s3 << std::endl;
- s3 += "abcd"; // 11112222abcd
- std::cout << "(s3 += 'abcd') = " << s3 << std::endl;
字符串比较,和逻辑运算符:
- #include
-
- const std::string s1("1111");
- const std::string s2("2222");
- // 比较字符串:(s1
s2)=1 - const int cmp = s1.compare(s2);
- std::cout << s1 << " comapre " << s2 << " = " << cmp << std::endl;
- // 五个逻辑运算:>,>=,<,<=,==
- // 例子如下:
- if (s1 == s2) {
- // 判断s1==s2
- std::cout << s1 << " == " << s2 << std::endl;
- }
- else if (s1 > s2) {
- // 判断s1>s2
- std::cout << s1 << " > " << s2 << std::endl;
- }
- else if (s1 < s2) {
- // 判断s1
- std::cout << s1 << " < " << s2 << std::endl;
- }
- #include
-
- const std::string s1("1111");
- const std::string s2("222211113333aaaa456");
- // 计算字符长度
- size_t len1 = s1.length(); // 或s2.size()
- std::cout << "s1:" << s1 << ", length:" << len1 << std::endl;
- // 查找字符
- const size_t findIndex1 = s2.find('1'); // 查找子字符,找不到返回 std::string::npos
- std::cout << s2 << " find char " << '1' << " = " << findIndex1 << std::endl;
- // 查找子字符串
- const size_t findIndex2 = s2.find(s1, 2); // 从第2位开始查找子字符串,找不到返回 std::string::npos
- std::cout << s2 << " find string " << s1 << " = " << findIndex2 << std::endl;
- const size_t findIndex3 = s2.rfind("3333"); // 从右到左开始查找子字符串,找不到返回 std::string::npos
- std::cout << s2 << " rfind string '3333' " << " = " << findIndex3 << std::endl;
- // 字符串拼接
- std::string s5 = s1 + s2;
- std::cout << s1 << " + " << s2 << " = " << s5 << std::endl;
- s5.append("ABCD");
- std::cout << "(s5.append('ABCD') = " << s5 << std::endl;
- // 截取子字符串
- const std::string s6 = s2.substr(2); // 从第2个字符开始,取后面所有字符=2211113333aaaa456
- std::cout << "s6:" << s6 << std::endl;
- std::string s7 = s2.substr(2, 7); // 从第2个字符开始,取后面7个字符=2211113
- std::cout << "s7:" << s7 << std::endl;
- s7.clear(); // 清空字符串
- const bool s7IsEmpty = s7.empty(); // 判断是否空字符串
- std::cout << "s7 is empty:" << s7IsEmpty << std::endl;
- // 插入字符串
- s7 = "123456";
- s7.insert(2, "abcd"); // 在第2位插入abcd=12abcd3456
- std::cout << "s7:" << s7 << std::endl;
- // 替换字符串
- s7.replace(2, 5, "ABCD"); // 在第2位连续5个字符替换成ABCD=12ABCD456
- std::cout << "s7:" << s7 << std::endl;
- // 截取单独字符
- const char c1 = s7.at(2); // 取第2个字符=A
- std::cout << s7 << " at(2) = " << c1 << std::endl;
字符串排序是从 0 开始计算,即字符串 "abcd",第 0 位是 'a',第 1 位是 'b',依此类推;(实际上,所有数组相关序号都是从 0 开始排序)
使用方法跟 std::string 一样,参照使用即可
- 使用 C 语言字符串 (char s[N]),需要注意字符串 N 的长度,避免复制超过 N 长度字符串,发生访问越界导致程序异常问题
- 使用 std::string::at(INDEX) 函数,也需要避免 INDEX 超过字符串长度,否则也会程序异常问题
- 练习1:用 while 循环生成 'a'~'z' 26个字符的 s1 字符串,打印到屏幕上
- 练习2:把全部小字的字符串 s1,转成大字的 'A'~'Z' s2字符串,并打印字符串到屏幕上
- 练习3:分别从第 10 位到第 20 位,每次取出 5 个字符并打印出结果
进入视频号学习本文配套视频课程。
-【End】-
哈喽,大家好!我是喜欢coding的小二,一枚“靓仔又不掉头发”的C++开发大头兵;欢迎围观公众号(20YC编程),定期会分享一些技术开发经验、行业资讯、业界动态等。期待您的关注,一起交流学习进步。
#下载示例源码和开发工具,请进入公众号首页,点击(发消息)最下方的(资源下载)菜单即可获取。
喜欢本文章,记得点赞、分享、关注哦~