size()、length()、capacity():
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("test string");
// size()和length()的作用一样,返回当前字符串s的长度
cout << "size: " << str.size() << "\n";
cout << "length: " << str.length() << "\n";
// capacity() 当前分配给s的存储空间
cout << "capacity: " << str.capacity() << "\n";
return 0;
}
substr()
string substr(size_t pos = 0, size_t len = npos) const;
Returns a newly constructed string object with its value initialized to a copy of a substring of this object.
// string::substr
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos); // get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}