目录
三、string的插入:push_back()和insert()
九、string大小写转换:tolower() 和 toupper()
十、string数值转换:stoi() 、 atoi() 、 to_string() 和 c_str
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开始到字符串结束的位置作为字符串初值
- string str1; //生成空字符串
- string str2("123456789"); //生成"1234456789"的复制品
- string str3("12345", 0, 3); //结果为"123"
- string str4("012345", 5); //结果为"01234"
- string str5(5, '1'); //结果为"11111"
- string str6(str2, 2); //结果为"3456789"
size()和length():返回string对象的字符个数,他们执行效果相同
max_size():返回string对象最多包含的字符数,超出会抛出异常
capacity():重新分配内存之前,string对象能包含的最大字符数
- string str("1234567");
- cout << "size=" << s.size() << endl; // size=7
- cout << "length=" << s.length() << endl; // length=7
- cout << "max_size=" << s.max_size() << endl; // max_size=4294967294
- cout << "capacity=" << s.capacity() << endl; // capacity=15
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
- // 在s1尾插一个字符
- s1.push_back('a');
-
-
- // insert(pos,char):在制定的位置pos前插入字符char
- s1.insert(s1.begin(),'1');
-
- string str="hello";
- string s="Hahah";
- str.insert(1,s);//在str下标为1的字符e前插入字符串s
-
- string str1="hello";
- char c='w';
- str1.insert(4,5,c);//在原串下标为4的字符o前插入5个字符c
-
- string str2="hello";
- string s2="weakhaha";
- str2.insert(0,s2,1,3);//将字符串s2从下标为1的e开始数3个字符,分别是eak,插入原串的下标为0的字符h前
bool operator==(const string &s1,const string &s2) const; //比较两个字符串是否相等
运算符">","<",">=","<=","!="均被重载用于字符串的比较;
用于比较字符串。compare 成员函数有以下返回值:
compare()比较时逐字符比较的,一旦能比较出结果,就不再比较了。
- string s1="abandon";
- string s2="about";
- int b=s1.compare(s2);//直接比较,s1小于s2,故返回-1
- cout<
-
- int c=s1.compare(2,4,s2);//s1下标为2的字符a开始的4个字符ando和s2进行比较。ando大于s2故返回1
- cout<
-
- int d=s1.compare(2,4,s2,1,3);
- cout<
//s1下标为2的字符a开始的4个字符ando和s2下标为1的字符b开始的3个字符bou比较。前者小,故返回-1。 -
- string s3="abc";
- string s4="abc";
- int e=s3.compare(s4);//相等返回0
- cout<
五、string拼接字符串:append()、+=
- //方法一:append()
- string s1("abc");
- s1.append("def");
- cout<<"s1:"<
// s1:abcdef -
- // 方法二:+= 操作符
- string s2 = "abc";
- //s2 += "def";
- string s3 = "def";
- s2 += s3.c_str();
- 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():删除字符串中所有字符
- string str = "123456789";
-
- str.erase(3, 3);
- cout<
// 123789 -
- str.erase(str.begin() + 3);
- cout<
// 12356789 -
- str.erase(str.begin() + 3, str.begin() + 6);
- cout<
// 123789
七、string遍历:迭代器或下标
- string s1("abcdef");
-
- // 正向迭代器
- string::iterator iter = s1.begin();
- for( ; iter < s1.end() ; iter++)
- {
- cout<<*iter;
- }
- cout<
//abcdef -
- // 反向迭代器
- string::reverse_iterator riter = s1.rbegin();
- for( ; riter < s1.rend() ; riter++)
- {
- cout<<*riter;
- }
- 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
- string s1("hello,world!");
-
- s1.replace(6, 5, "girl"); // 结果:hello,girl.
- s1.replace(s1.size() - 1, 1, 1, '.'); // 结果:hello,world.
- s1.replace(s1.begin(), s1.begin() + 5, "boy"); // 结果:boy,girl.
九、string大小写转换:tolower() 和 toupper()
tolower(char) 、 toupper(char) :将字符进行大小写转换
- string s = "ABCDEFG";
-
- for( int i = 0; i < s.size(); i++ )
- {
- s[i] = tolower(s[i]);
- }
- cout << s << endl; //abcdefg
-
- transform(s.begin(), s.end(), s.begin(), ::toupper);
- 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*
- string s1("1234567");
- char* s2 = "1234567";
-
- int a = stoi(s1);
- int b = atoi(s2);
- int c = atoi(s1.c_str());
- cout << a << endl;
- cout << b << endl;
- cout << c << endl;
- int a = 4;
- double b = 3.14;
- string str1, str2;
- str1 = to_string(a);
- str2 = to_string(b);
- cout << str1 << endl;
- cout << str2 << endl;
虽然 C++ 提供了 string 类来替代C语言中的字符串,但是在实际编程中,有时候必须要使用C风格的字符串(例如打开文件时的路径)
- string path = "D:\\demo.txt";
- FILE *fp = fopen(path.c_str(), "rt");
十一、string的查找:find()
- /*
- string的find()函数用于找出字母在字符串中的位置。
- find(str,position)
- find()的两个参数:
- str:是要找的元素
- position:字符串中的某个位置,表示从从这个位置开始的字符串中找指定元素。
- 可以不填第二个参数,默认从字符串的开头进行查找。
- 返回值为目标字符的位置,当没有找到目标字符时返回npos。
- */
-
- string s = "hello world!";
- cout << s.find("e") << endl; // 1
-
- string s = "hello world!";
- if (s.find("a") == s.npos) {
- cout << "404 not found" << endl;
- }
-
- string s = "hello world!";
- cout << s.find("l",5) << endl; //9
-
- //找到目标字符在字符串中第一次出现和最后一次出现的位置
- string s = "hello world!";
- cout << "first time occur in s:"<
find_first_of("l") << endl; // 2 - cout << "last time occur in s:" << s.find_last_of("l") << endl; // 9
-
- //反向查找
- string s = "hello world!";
- 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
- #include
- #include
- using namespace std;
-
- int main(){
- string s1 = "first second third";
- string s2;
- s2 = s1.substr(6, 6);
- cout<< s1 <
- cout<< s2 <
- return 0;
- }
若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