【C++程序员必修课】C++基础课程-10:C++ 字符串

1 本课主要内容:

  • 字符串的使用场景?
  • C 风格字符串 (char*) 的定义,及常用函数使用
  • C++ 标准库字符串 (std::string) 及常用函数使用

2 主动知识点:

  • 字符串的使用场景?

字符串类型可以用来表示各种名称、地址、描述、备注等文本数据;主要有 C 风格字符串(char*)和 C++ 风格字符串(std::string、std::wstring)二种字符串;

  • C 风格字符串 (char*) 的定义

例子如下:

#include <string.h>

// 例子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;
  • C 风格字符串常用函数使用

#include <string.h>

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; (s1==s2)=0; (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;
  • C++ 字符串 (std::string)

初始化字符串

#include <string>

// 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 <string>

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 <string>

const std::string s1("1111");
const std::string s2("2222");
// 比较字符串:(s1<s2)=-1; (s1==s2)=0; (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<s2
    std::cout << s1 << " < " << s2 << std::endl;
}
  • std::string 常用函数例子

#include <string>

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 开始排序)

  • 字符串结束符标识 (\0)

  • std::wstring 宽字符串类

使用方法跟 std::string 一样,参照使用即可

3 注意事项:

  • 使用 C 语言字符串 (char s[N]),需要注意字符串 N 的长度,避免复制超过 N 长度字符串,发生访问越界导致程序异常问题
  • 使用 std::string::at(INDEX) 函数,也需要避免 INDEX 超过字符串长度,否则也会程序异常问题

4 课后练习:

  • 练习1:用 while 循环生成 'a'~'z' 26个字符的 s1 字符串,打印到屏幕上
  • 练习2:把全部小字的字符串 s1,转成大字的 'A'~'Z' s2字符串,并打印字符串到屏幕上
  • 练习3:分别从第 10 位到第 20 位,每次取出 5 个字符并打印出结果

  附录:在线视频课程

进入视频号学习本文配套视频课程。

-【End】-

哈喽,大家好!我是喜欢coding的小二,一枚“靓仔又不掉头发”的C++开发大头兵;欢迎围观公众号(20YC编程),定期会分享一些技术开发经验、行业资讯、业界动态等。期待您的关注,一起交流学习进步。

#下载示例源码和开发工具,请进入公众号首页,点击(发消息)最下方的(资源下载)菜单即可获取。

喜欢本文章,记得点赞、分享、关注哦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天恩软件工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值