c ++字符串向量,删除字符行为怪异

时间:2014-02-08 11:37:12

标签: c++ substring erase

我正在尝试为我的数据编写自己的json解析器。切割时,单个条目看起来像这样

`"ID":"612","path":"letter_2013-12-16 17:49:43  0000.png","longitude":"24.9767","latitude":"60.2099","letter":"K"`

现在我正在尝试将这些放入另一个向量中,我将所需的单个数据剪切掉。因为我没有找到一种方法来删除“来自c ++中的字符串(建议欢迎),我将按如下方式进行:

individualEntries=ofSplitString(fullResponse, "},{");
for(int i=0; i<individualEntries.size(); i++){
    printf("entry %s \n",individualEntries[i].c_str());
    vector<string> cutEntries =ofSplitString(individualEntries[i], ",");
    for(int j=0; j<cutEntries.size(); j++){
        //delete "ID":"
        cutEntries[0].erase (0, 6);
        //delete "
        cutEntries[0]=cutEntries[0].substr(0,cutEntries[0].length()-1);

        //delete "path":"
        cutEntries[1].erase(0,4);
        cutEntries[1]=cutEntries[1].substr(0,cutEntries[1].length()-1);
        //cutEntries[1].append("g");
        printf("%s ",cutEntries[j].c_str());
    }
    printf("\n");
}

结果如下:

1159 letter_2014-01-19 12:59:05  0000.pn "longitude":"25.0903" "latitude":"60.1956" "letter":"I" 

它适用于第一部分,我很好地剪切了我的ID,但是当我试图切出“路径”时:“它开始表现得很奇怪。长度应该是长度8,但是切出4会达到当做子串删除最后一个字母时,它会删除最后一个字母。奇怪的是第一个条目(cutEntries [0])不会发生同样的情况,但只发生在第二个条目(cutEntries [1])任何想法?

2 个答案:

答案 0 :(得分:0)

此循环

for(int j=0; j<cutEntries.size(); j++){
    //delete "ID":"
    cutEntries[0].erase (0, 6);
    //delete "
    cutEntries[0]=cutEntries[0].substr(0,cutEntries[0].length()-1);

    //delete "path":"
    cutEntries[1].erase(0,4);
    cutEntries[1]=cutEntries[1].substr(0,cutEntries[1].length()-1);
    //cutEntries[1].append("g");
    printf("%s ",cutEntries[j].c_str());
}

没有任何意义,因为你至少cutEntries.size()次正在做

    cutEntries[0].erase (0, 6);
    //delete "
    cutEntries[0]=cutEntries[0].substr(0,cutEntries[0].length()-1);

此操作应在循环外完成,或者您可以编写例如

if ( j == 0 )
{

   cutEntries[j].erase (0, 6);
    //delete "
    cutEntries[j]=cutEntries[j].substr(0,cutEntries[j].length()-1);
}

但无论如何这是一个糟糕的设计。

答案 1 :(得分:0)

这实现了目标:

fullResponse=ofToString(response.data);
//printf("urlResponse %s \n",fullResponse.c_str());
ofStringReplace(fullResponse, "[{", "");
ofStringReplace(fullResponse, "}]", "");
individualEntries=ofSplitString(fullResponse, "},{");
for(int i=0; i<individualEntries.size(); i++){
    printf("entry %s \n",individualEntries[i].c_str());
    vector<string> cutEntries =ofSplitString(individualEntries[i], ",");
    //delete the first parts in all of them
        ofStringReplace(cutEntries[0], "\"ID\":\"", "");
        ofStringReplace(cutEntries[1], "\"path\":\"", "");
        ofStringReplace(cutEntries[2], "\"longitude\":\"", "");
        ofStringReplace(cutEntries[3], "\"latitude\":\"", "");
        ofStringReplace(cutEntries[4], "\"letter\":\"", "");
    //delete the last " in all of them
    ofStringReplace(cutEntries[0], "\"", "");
    ofStringReplace(cutEntries[1], "\"", "");
    ofStringReplace(cutEntries[2], "\"", "");
    ofStringReplace(cutEntries[3], "\"", "");
    ofStringReplace(cutEntries[4], "\"", "");

    for(int j=0; j<cutEntries.size(); j++){
        printf("%s ",cutEntries[j].c_str());
    }
    printf("\n");
}