string
table list
someting else basic function
init
string test1; //空串
string test2 = "内容"; //赋值=
string test3("内容"); //使用引用字符数组作为参数传给构造函数
string test4(test2); //用一个string初始化另一而string
string test5(test2,pos,num); //从test2中的第pos个位置开始,拷贝个数为num个字符
string test6 = test2 + "内容" + test3 //混合初始化
string test7 = test2.substr(pos,num); //从test2中的第pos个位置开始,拷贝个数为num个字符
string test8 = test2.substr(); //参数列表为空则会拷贝test2的整个对象(复制test2的简便方法)
string test9(num,ch); //拷贝num个字符型ch到test9
std::basic_string::find
#include <string>
#include <iostream>
void print(std::string::size_type n, std::string const &s)
{
if (n == std::string::npos) {
std::cout << "not found
";
} else {
std::cout << "found: " << s.substr(n) << '
';
}
}
int main()
{
std::string::size_type n;
std::string const s = "This is a string";
// search from beginning of string
n = s.find("is");
print(n, s);
// search from position 5
n = s.find("is", 5);
print(n, s);
// find a single character
n = s.find('a');
print(n, s);
// find a single character
n = s.find('q');
print(n, s);
}
found: is is a string
found: is a string
found: a string
not found