【发布时间】:2016-03-20 03:33:33
【问题描述】:
我可以得到字符串的前半部分:
insert1 = tCreatureOne.substr(0, (tCreatureOne.length) / 2
我不知道如何获取字符串的后半部分
insert2 = tCreatureOne.substr((tCreatureOne.length) / 2), ?????)
这是我的代码。
// Insert creature two in to the
//middle of creature one.Science!
// Hamster and Emu make a HamEmuster
std::string PerformScience(std::string tCreatureOne, std::string tCreatureTwo)
{
std::string insert1;
std::string insert2;
std::string insert3;
// first half : 0 to middle
insert1 = tCreatureOne.substr(0, (tCreatureOne.length) / 2);
// last half: from middle to the end
insert2 = tCreatureOne.substr((tCreatureOne.length) / 2), tCreatureOne.length);
insert3 = insert1 + tCreatureTwo + insert2;
return insert3;
【问题讨论】:
-
您不能不再次致电
substr()!签出this。 -
来自cplusplus.com/reference/string/string/substr 我怀疑你正在寻找
string::npos。 IE。insert2 = tCreatureOne.substr((tCreatureOne.length) / 2), std::string::npos); -
@CraigYoung
std::string::npos已经是默认设置。根本不需要指定第二个参数。