级联
你可以使用重载的+
和+=
运算符连接 std::string
s。使用+
运算符:
std::string hello = "Hello";
std::string world = "world";
std::string helloworld = hello + world; // "Helloworld"
使用+=
运算符:
std::string hello = "Hello";
std::string world = "world";
hello += world; // "Helloworld"
你还可以附加 C 字符串,包括字符串文字:
std::string hello = "Hello";
std::string world = "world";
const char *comma = ", ";
std::string newhelloworld = hello + comma + world + "!"; // "Hello, world!"
你也可以使用 push_back()
来推回个人 char
s:
std::string s = "a, b, ";
s.push_back('c'); // "a, b, c"
还有 append()
,非常像+=
:
std::string app = "test and ";
app.append("test"); // "test and test"