std :: string - 从头到尾获取数据

时间:2010-01-28 18:20:10

标签: c++ stdstring

我想在两个索引之间获取一些数据。 例如,我有字符串:“只是测试......”我需要从2到6的字符串。我应该得到: 'st tes'。

我能做什么功能?

2 个答案:

答案 0 :(得分:3)

使用substr

std::string myString = "just testing...";

std::string theSubstring = myString.substr(2, 6);

请注意,第二个参数是子字符串的 length ,而不是索引(您的问题有点不清楚,因为从2到6的子字符串实际上是'st t',而不是'st' TES')。

答案 1 :(得分:1)

使用substr方法:

std::string theString = "just testing";
std::string theSubstring = theString.substr(2, 4);
相关问题