繁体   English   中英

流出字符串的一部分

[英]Stream Out Part of a string

我想流式输出std::string但我希望能够不使用前两个字符或后两个字符。

例如:

string foo{"bu blah blah blee le"};

cout << foo.substr(2) << endl << foo.substr(0, foo.size() - 2) << endl;

有为此使用的iomanip工具吗? 还是应该继续构建临时string s?

您可以使用cout.write

cout.write(foo.c_str() + 2, foo.size() - 4);

这也会返回流,因此您可以执行以下操作:

cout << "First two: ";
cout.write(foo.c_str(), 2) << "\nAll but first two: ";
cout.write(foo.c_str() + 2, foo.size() - 2) << '\n';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM