Splitting a string by a character

前端 未结 8 999
夕颜
夕颜 2020-11-27 05:18

I know this is a quite easy problem but I just want to solve it for myself once and for all

I would simply like to split a string into an array using a character as

8条回答
  •  臣服心动
    2020-11-27 05:55

    Using vectors, strings and stringstream. A tad cumbersome but it does the trick.

    std::stringstream test("this_is_a_test_string");
    std::string segment;
    std::vector seglist;
    
    while(std::getline(test, segment, '_'))
    {
       seglist.push_back(segment);
    }
    

    Which results in a vector with the same contents as

    std::vector seglist{ "this", "is", "a", "test", "string" };
    

提交回复
热议问题