【编程语言系列】1、字符串 (C、C++)

本文详细介绍了C和C++中字符串的操作方法,包括创建、获取长度、复制、连接、比较、查找、大小写转换等功能,并提供了丰富的代码示例。

文章目录

C

C 语言中,字符串实际上是使用 == ‘\0’ ==字符终止的一维字符数组

#include <string.h>

> 1. 创建
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char greeting[] = "Hello";

> 2. 获取字符串长度
len = strlen(str1);

> 3. 复制:
strcpy(str1, str2); //把str2复制到str1

> 4. 连接:
strcat(str1, str2); //连接str2到str1尾部
 
> 5. 比较:
strcmp(s1, s2); //相同的,则返回 0;s1<s2 返回<0 ;s1>s2 则返回>0

> 6. 查找
strchr(s1, "s"); //返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置。
strstr(s1, s2); //返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置

> 7.大小写转化
s[i] = tolower(s[i]); //转小写
s[i] = toupper(s[i]); //转大写

C++

#include <string>

> 1. 创建
string str = "hello world";

> 2. 获取字符串长度
len = s.size();
len = s.length(); //返回string对象的字符个数,执行效果同上
s.max_size(); //返回string对象最多包含的字符数
s.capacity(); //重新分配内存之前,string对象能包含的最大字符数

> 3. 复制:
string s1(str);  //调用复制构造函数生成s1,s1为str的复制品
string s2(str,6);   //将str内,开始于位置6的部分当作s2的初值
string s3(str,6,3);  //将str内,开始于6且长度顶多为3的部分作为s3的初值
string s4(cstr);   //将C字符串作为s4的初值(char cstr[] = "abcde";)
string s5(cstr,3);  //将C字符串前3个字符作为字符串s5的初值。
string s6(5,'A');  //生成一个字符串,包含5个'A'字符
string s7(str.begin(),str.begin()+5); //区间str.begin()和str.begin()+5内的字符作为初值

> 4. 连接:
s1.append("def");  // 方法一:append()
s2 += s3.c_str(); //方法二:+ 操作符
 
> 5. 比较:
A.compare(B) //A和B比较,0:相等 1:大于 -1:小于
str == "Li"; // C ++字符串支持常见的比较操作符(>,>=,<,<=,==,!=)
str < "Li";

> 6. 查找
strchr(s1, "s"); //返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置。
strstr(s1, s2); //返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置

> 7. 插入
s1.push_back('a'); // 尾插一个字符
s1.insert(s1.begin(),'1');  //在制定的位置pos前插入字符char

> 8. 删除
s.erase(iterator p);//删除字符串中p所指的字符
s.erase(iterator first, iterator last); //删除字符串中迭代器
s.erase(size_t pos = 0, size_t len = npos);//删除字符串中从索引
s. clear();//删除字符串中所有字符

> 9. 遍历
for( int i = 0; i < s1.size() ; i++ ){} //下标法
for(string::iterator iter = s1.begin() ; iter < s1.end() ; iter++){} //迭代器

> 10. 替换
s1.replace(3,2,2,'h');   //将当前字符串从3开始的2个字符,替换成2个字符c
s1.replace(6,5,"girl");  //从6索引开始的5个字符,替换成字符串s
s1.replace(s1.begin(),s1.begin()+5,"boy"); //将当前字符串[i1,i2)区间中的字符串替换为字符串s
  
> 11. 大小写转换
transform(s.begin(),s.end(),s.begin(),::tolower);

> 12. 查找
s.find("chicken");// 1. 查找一个字符串
s.find('i',6)// 2. 从下标为6开始找字符'i',返回找到的第一个i的下标
s.rfind("chicken")  // 3. 从字符串的末尾开始查找字符串,返回的还是首字母在字符串中的下标
s.rfind('i') // 4. 从字符串的末尾开始查找字符
s.find_first_of("13br98") // 5. 在该字符串中查找第一个属于字符串s的字符
s.find_first_not_of("hello dog 2006")  // 6. 在该字符串中查找第一个不属于字符串s的字符
s.find_last_of("13r98") // 7. 在该字符串最后中查找第一个属于字符串s的字符
s.find_last_not_of("teac")  // 8. 在该字符串最后中查找第一个不属于字符串s的字符-

> 13. 截取
const char *split = ",; !";
char *p2 = strtok(str,split); //根据指定分隔符分割
s1.substr(2,5) //从2开始截取长度为5的字符串

> 14. 排序
sort(s.begin(),s.end());

参考:https://blog.youkuaiyun.com/qq_37941471/article/details/82107077
Welcome to Beginning C++17. This is a revised and updated version of Ivor Horton’s original book called Beginning ANSI C++. The C++ language has been extended and improved considerably since then, so much so that it was no longer possible to squeeze detailed explanations of all of C++ into a single book. This tutorial will teach the essentials of the C++ language and Standard Library features, which will be more than enough for you to write your own C++ applications. With the knowledge from this book, you should have no difficulty in extending the depth and scope of your C++ expertise. We have assumed no prior programming knowledge. If you are keen to learn and have an aptitude for thinking logically, getting a grip on C++ will be easier than you might imagine. By developing C++ skills, you’ll be learning a language that is already used by millions and that provides the capability for application development in just about any context. C++ is very powerful. Arguably, it’s more powerful than most programming languages. So, yes, like with any powerful tool you can wield some considerable damage if you use it without proper training. We often compare C++ to a Swiss Army knife: age-old, trusted, incredibly versatile, yet potentially mind-boggling and full of pointy things that could really hurt you. Once someone clearly explains to you what all the different tools are meant for, however, and teaches you some elementary knife safety rules, then you’ll never have to look for another pocketknife again. C++ does not need to be dangerous or difficult at all either. C++ today is much more accessible than many people assume. The language has come a long way since its conception nearly 40 years ago. In essence, we have learned how to wield all its mighty blades and tools in the safest and most effective way possible. And, more importantly perhaps, the C++ language and its Standard Library have evolved accordingly to facilitate this. The past decade in particular has seen the ris
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值