这是文本
" Abraham,Yohannes A。",员工,
我想要第二个逗号的索引
这是我的代码int foundStatus = tempString.find_first_of(",", 8);
我认为这将返回索引8之后的第二个逗号的索引,即22,但是当我给我-1时
答案 0 :(得分:0)
您需要使用9作为第二个参数调用std::string::find_first_of,而不是8(假设您的字符串为:"\"Abraham, Yohannes A.\", Employee, "
)。
否则它将返回8而不是22。
返回值std::string::npos( - 1)表示之后找不到任何逗号 - 检查输入字符串两次,在第8位后确实有第二个逗号。
以下代码将正确输出22:
#include <iostream>
#include <string>
int main(int argc, char* argv[]){
std::string tempString = "\"Abraham, Yohannes A.\", Employee, ";
size_t firstComma = tempString.find_first_of(","); // firstComma = 8
if (firstComma != std::string::npos) {
size_t secondComma = tempString.find_first_of(",", firstComma+1); // secondComma = 22
std::cout << secondComma << std::endl;
}
return 0;
}