ITeye博客

std::string的基本用法

mmdev · 2004-07-06 00:01 · Markdown

可以直接相加:

string s1 = "Hello";
string s2 = "world";
string s3 = s1 + ", " + s2 + "!\n";

可以附加:

s1 += '\n';

可以比较:

if (s1==s2) {
...
}
else if(s1=="yes") {
...
}

可以操作子串:

string name = "Niels Stroustrup";
string s = name.substr(6, 10); // s = "Stroustrup"
name.replace(0, 5, "Nicholas"); // name becomes "Nicholas Stroustrup"

转换为C-Style String:

printf("name: %s", name.c_str());

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。