string
- string的赋值
- 函数
- to_string()
- 大小写转化问题
string的赋值
(1) 直接给字符串对象赋值
string s;
s = "Hello, everybody";
(2)将字符指针赋给一个字符串对象
string s;
char ss[100];//字符数组
scanf("%s", &ss);
s = ss;
(3)将string赋给string
string s, ss;
ss = "123456";
s = ss;
(4) 初始化
string s;
s = string(6, 'k');//拷贝初始化,kkkkkk
string ss("abcd");//不可以写为string ss; ss("abcd");
(5)在string对象尾部添加字符
string s;
s += 'a';
s = s + 'b';
(6)在string对象尾部添加字符串
string s;
s += "abc";//直接使用+
s.append("def");//使用函数append()
函数
1)insert()将字符插入string
string s;
s = "123456";
string::iterator it;
it = s.begin();
//s.insert(it + 3, "abc");//错误,不可以插入字符串
s.insert(it + 5, 'k');//把字符p插入到第5个字符后
/*12345k6*/
str1.insert(2,str2,0,4); //将str2脚标0开始的连续4个字符插入到str1脚标2开始的地方
2)删除string内的元素
1‘ 删除第i+1个元素[erase()]
string s;
s = "abcdef";
string::iterator it = s.begin();
s.erase(it + 3);//删除掉第3+1个元素,//abcef
2’ 删除下标从i到j-1的元素[erase()]
string s;
s = "abcdef";
string::iterator it = s.begin();
s.erase(it + 1, it + 4);//删除下标为1到4-1的元素//aef
3‘ 清空整个字符串
string s;
s = "abcdef";
string::iterator it = s.begin();
s = "";//清空字符串
3) 判断string字符串是否为空[empty()]
string s;
s = "abcdef";
if (s.empty()) cout << "yes" << endl;
else puts("no");
//no
4)访问string对象的元素
string s;
s = "abcdef";
cout << s[0] << endl;//a
cout << s[0] - 'a' << endl;//0
cout << s << endl;//abcdef
5) string 对象的长度[length()/size()]
string s;
s = "abcdef";
cout << s.length() << endl;//6
cout << s.size() << endl;//6
6)搜索string对象的元素或者字串[find()]
string s;
s = "abcdef";
cout << s.find('d') << endl;//查找到第一个字符'd',返回其下标值//3
cout << s.find("d") << endl;//查找到第一个子串"d",返回其下标值//3
cout << s.find("de") << endl;//查找到第一个子串"de",返回其下标值//3
cout << s.find('k') << endl;//查找到第一个字符'k',返回其下标值//18446744073709551615
cout << s.find("k") << endl;//查找到第一个子串"k",返回其下标值//18446744073709551615
cout << s.find("jk") << endl;//查找到第一个子串"jk",返回其下值//18446744073709551615
string::npos
if (s.find(str) != string::npos)cout << s.find(str) << endl;
else cout << "-1" << endl;
7) 替换string对象的字符[replace()]
string s;
s = "abcdef";
s.replace(2,2,"kkkkk");//abkkkkkef
//从下标为2的元素开始,将连续的2个字符替换为kkkkk,也就是将cd替换为kkkkk
8)string对象的比较[compare()]
string s;
s = "abcdef";
//compare返回s比要比较的从头开始,多了或少了多少个
cout << s.compare("ab") << endl;//4
cout << s.compare("abcdef") << endl;//0
cout << s.compare("abcdefg") << endl;//-1
cout << s.compare("defg") << endl;//-3
cout << s.compare("b") << endl;//-1
string ss = "cd";
cout << s.compare(ss) << endl;//-2
9) 将string对象反向排序[reverse]
注意使用头文件algorithm
string s;
s = "abcdef";
reverse(s.begin(), s.end());//fedcba
10) 用vector存入string
vector<string> v;
v.push_back("abc");
v.push_back("def");
v.push_back("ghi");
for (auto it : v) cout << it << endl;
/*
abc
def
ghi
*/
11) 用printf输出string
string s;
s = "abcdef";
printf(s.c_str());//abcdef
12)复制字符字串[substr()]
//使用格式:str = s.substr(pos, n)
string s = "abcdefg";
string str = s.substr(2,3);
cout << str << endl;
//输出结果为
//cde
to_string()
将其他类型的数据转化为string类型
string s = to_string(t);
大小写转化问题
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
//transform(first, last, result, op);
//first是容器的首迭代器,last为容器的末迭代器,result为存放结果的容器
//op为要进行操作的一元函数对象或者sturct、class。
int main(){
string s = "Hello World";
cout << s << endl;
//toupper大写
transform(s.begin(), s.end(), s.begin(), ::toupper);
cout << s << endl;
//tolower小写
transform(s.begin(), s.end(), s.begin(), ::tolower);
cout << s << endl;
return 0;
}