1

I have ;

 string  str = "";

in str, I store data is in following form "_ : _, _ " "_" can be just a word like "X" or a composition of words like "a sds 23 dsds 1"

I want them parse it to three different string ;

in str       : X:y dfj kd kk,sdd 12 89 ++
string parsed[i] ;
in parsed[0] : X
in parsed[1] : y dfj kd kk
in parsed[2] : sdd 12 89 ++

How can I do that over using c++ std::string features ?

4

2 Answers 2

1

You can split your string using the following std:string methods for example:

size_t index1 = str.find( ":" ) + 1;
size_t index2 = str.find( ",", index1 ) + 1;

std::string sub1 = str.substr (0, index1-1);
std::string sub2 = str.substr (index1, index2-index1-1);
std::string sub3 = str.substr (index2, str.length()-index2);
Sign up to request clarification or add additional context in comments.

Comments

0

Using boost/algorithm/string.hpp

std::string str = "X:y dfj kd kk,sdd 12 89 ++"
std::vector<std::string> v;
boost::split(v, str, boost::is_any_of(":,"));

You can also use it for multibyte strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.