概念:
string本质上是一个类,内部封装了char*指针,内部管理内存的分配与释放
构造函数:
- string() 默认构造函数,创建一个空字符串
- string(const char* s) 使用字符串初始化
- string(const string& str); 使用一个string对象初始化一个新string对象
- string(int n,char c) 使用n个字符初始化
#include <iostream>
#include <string>
int main()
{
//默认构造函数,创建一个空字符串
std::string s1;
std::cout << "s1: " << s1 << std::endl;
//使用字符串初始化
const char* str = "Hello World!";
std::string s2(str);
std::cout << "s2: " << s2 << std::endl;
//使用一个string对象初始化一个新string对象
std::string s3(s2);
std::cout << "s3: " << s3 << std::endl;
//使用n个字符初始化
std::string s4(12, 'a');
std::cout << "s4: " << s4 << std::endl;
system("pause");
return 0;
}
字符串拼接:
- 通过+操作符
- 使用append成员方法
#include <iostream>
#include <string>
int main()
{
std::string s1 = "I";
std::string s2 = " LOVE";
//使用+操作符进行拼接两个字符串
std::cout << "s1+s2 = " << s1 + s2 << std::endl;
//把字符串完整的拼接至当前字符串结尾
s1.append(s2);
std::cout << "s1 = " << s1 << std::endl;
//把字符串的前n个字符拼接至当前字符串结尾
s1.append(" CHINA !", 6);
std::cout << "s1 = " << s1 << std::endl;
system("pause");
return 0;
}
元素访问:
- 通过[]操作符进行读写
- 通过成员方法at进行读写
- 通过c_str返回指向空终止字符串的指针
- 通过front访问首字符
- 通过back访问尾字符
#include <iostream>
#include <string>
int main()
{
std::string str = "Hello World!";
//通过[]操作符进行读写
for (int i = 0; i < str.size(); ++i)
std::cout << str[i];
std::cout << std::endl;
//通过成员方法at进行读写
for (int i = 0; i < str.size(); ++i)
std::cout << str.at(i);
std::cout << std::endl;
//通过c_str返回指向空终止字符串的指针
const char* c1 = str.c_str();
std::string str1(c1);
std::cout << str1 << std::endl;
//通过front访问首字符
char c2 = str.front();
std::cout << c2 << std::endl;
//通过back访问尾字符
char c3 = str.back();
std::cout << c3 << std::endl;
system("pause");
return 0;
}
容量:
- empty //检查字符串是否为空,若为空则返回true,否则返回false
- size / length //返回字符串的字符数
- capacity //返回当前对象所分配可用于存储元素的存储的容量
#include <iostream>
#include <string>
int main()
{
std::string str1 = "Hello World!";
std::string str2;
std::boolalpha(std::cout);
std::cout << "str1.empty = " << str1.empty() << std::endl;
std::cout << "str2.empty = " << str2.empty() << std::endl;
std::cout << "str1.size = " << str1.size() << std::endl;
std::cout << "str2.size = " << str2.size() << std::endl;
std::cout << "str1.length = " << str1.length() << std::endl;
std::cout << "str2.length = " << str2.length() << std::endl;
std::cout << "str1.capacity = " << str1.capacity() << std::endl;
std::cout << "str2.capacity = " << str2.capacity() << std::endl;
system("pause");
return 0;
}
字符串比较:
- compare //逐字符比较两个字符串是否相等,若相等则返回0,小于返回-1,大于返回1
#include <iostream>
#include <string>
int main()
{
std::string str1 = "Hello World";
std::string str2 = "Hello World";
if (str1.compare(str2) == 0)
std::cout << "str1与str2相等" << std::endl;
else if (str1.compare(str2) > 0)
std::cout << "str1大于str2" << std::endl;
else if (str1.compare(str2) < 0)
std::cout << "str1小于str2" << std::endl;
system("pause");
return 0;
}
子字符串提取:
- substr(pos) //返回 [pos,size()]
- substr(pos,count) //返回 [pos,pos+count],若pos+count超出size(),则返回 [pos,size()]
#include <iostream>
#include <string>
int main()
{
std::string str = "Hello World";
//count为npos,返回[pos,size()]
std::string str1 = str.substr(6);
std::cout << "str1 = " << str1 << std::endl;
//pos+count<size(),返回[pos,pos+count]
std::string str2 = str.substr(6, 3);
std::cout << "str2 = " << str2 << std::endl;
//pos<size(),pos+count>size(),返回[pos,size()]
std::string str3 = str.substr(6, 10);
std::cout << "str3 = " << str3 << std::endl;
system("pause");
return 0;
}
查找:
- find //从前往后查找
- rfind //从后往前查找
- find_first_of //从前往后查找字符的首次出现
- find_last_of //从前往后查找字符的最后一次出现
若查找到返回相应的pos,查找不到则返回std::string::npos,该值为特殊值,等于size_type类型可表示的最大值。
#include <iostream>
#include <string>
int main()
{
std::string str = "aabbccbbaa";
std::string::size_type find_pos = str.find('b');
std::string::size_type rfind_pos = str.rfind('b');
std::string::size_type first_pos = str.find_first_of('b');
std::string::size_type last_pos = str.find_last_of('b');
if (find_pos != std::string::npos)
std::cout << "找到字符: find_pos = " << find_pos << std::endl;
else
std::cout << "字符串不存在该字符" << std::endl;
if (rfind_pos != std::string::npos)
std::cout << "找到字符: rfind_pos = " << rfind_pos << std::endl;
else
std::cout << "字符串不存在该字符" << std::endl;
if (first_pos != std::string::npos)
std::cout << "找到字符: first_pos = " << first_pos << std::endl;
else
std::cout << "字符串不存在该字符" << std::endl;
if (last_pos != std::string::npos)
std::cout << "找到字符: last_pos = " << last_pos << std::endl;
else
std::cout << "字符串不存在该字符" << std::endl;
system("pause");
return 0;
}