【发布时间】:2015-07-17 20:29:44
【问题描述】:
int start = 0;
int end = 0;
string temp = "<sasadfsadfsady>40000</sadsfasdfsadflary>";
for (int i = 0; i < temp.length(); i++){
if (temp[i] == '>' && start == 0) //will only save first one
start = i;
if (temp[i] == '<')
end = i-start; //will be overwritten by the second one
}
temp.erase(temp.begin(), temp.begin()+start+1);
temp.erase(temp.begin() + end-1, temp.end());
cout << endl;
cout << temp << end;
输出:
400006
为什么最后是6?我不知道为什么会这样,请帮助我
【问题讨论】:
-
temp[i] == temp.length()没有多大意义。 “如果字符串中的字符与字符串的长度具有相同的值”。那应该是if (i == temp.length()),这也是不可能的,因为无论如何你的循环都会在 i >= temp.length() 时终止。