can you help me use the stl string class?

This topic is 4305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi. i have been reading my book on the stl string section but i am having much difficulty. this is what i need to do in c++, a string that will be passed to my function will look like this: "item 123/field 1 = something/ field 2 = something/ field 3 = something/item 234 / field 1 = something/ field 2 = something/ fields 3 = somethind/ item 125 / field 1 = something/ field 2 = something/ fields 3 = somethind/ ENDLINE/" how can i use the stl string funtions to extract the item number, and then extract each field for the item number so i can store them in a structure where i can update them whenever i need to?

Share on other sites
since i know field 1 is between the first instance of '/' and the second '/' and field 2 is between the second '/' and the third '/', how can i extract whats in between the '/' and the next '/'?

Share on other sites
The stl string class is cool, but it isn't magic. Look for the RegEx lib around the net. Or, use XML and use an XML parser to get that information.

The only way I know of to do it with a string is to write a parser for this information yourself.

Share on other sites
Write a little function that will split your string at the '/' characters. For instance, given "Field1 = 0/Field2 = 1"
It will return a std::vector<std::string> containing "Field1 = 0" and "Field2 = 1".
After that, loop through the vector, and for each string, extract what you need, and use atoi, atof for convert the value to what you need.

usefull strings methods for that will be : std::string::find and std::string::substr.

(I don't have time to write an example, sorry)

Share on other sites
Quote:
 Original post by paicWrite a little function that will split your string at the '/' characters. For instance, given "Field1 = 0/Field2 = 1"It will return a std::vector containing "Field1 = 0" and "Field2 = 1".After that, loop through the vector, and for each string, extract what you need, and use atoi, atof for convert the value to what you need.usefull strings methods for that will be : std::string::find and std::string::substr.(I don't have time to write an example, sorry)

Like this I guess:

void TokenizeString(std::string const & pString, std::string const & pDelimiter, std::vector <std::string> & pResult){	if(pString.empty() || pDelimiter.empty())		return void();	else if(!pResult.empty())		pResult.clear();	size_t stIndexA = size_t(), stIndexB = pString.find(pDelimiter);	if(stIndexB == std::string::npos)		return void();	while(stIndexB != std::string::npos)	{		pResult.push_back(pString.substr(stIndexA, stIndexB - stIndexA));		stIndexB = pString.find(pDelimiter, stIndexA = stIndexB + pDelimiter.size());	}	pResult.push_back(pString.substr(stIndexA, pString.size() - stIndexA));}

Edit: Forgot to say that this will only split the string where it finds the delimiter (if any), then you'll need to extract your data from the tokens in the vector.

Share on other sites
Quote:
 Original post by bakerhow can i use the stl string funtions to extract the item number, and then extract each field for the item number so i can store them in a structure where i can update them whenever i need to?
If you'd like to avoid reinventing the wheel, boost::tokenizer<> and boost::lexical_cast<> will do simple parsing and conversion for you. If you're doing this to learn though, writing a simple tokenizer yourself (as suggested above) shouldn't be too hard.

Share on other sites
right, tokenizing and parsing,

Share on other sites
but as the title suggests, can you help with STL string class
not boost::

you can try something like this: here I wrote something for you :D
function parse_data(string data,           //this is your string to be passed                       string separator,      //you want to separate it by '/'                    string data_array[256] //where you want stored                                           ){  int pos;  for( i=0; i<=255;i++ ){    pos=data.find(separator); //find the position where there is a /    if ( pos == 0 ) break;    //if data has run out of strings break;    data_array[i]=data.substr(0,pos); //save into array    data=data.substr(pos+1,data.length()); //change data excluding what was saved  }}

perhaps instead of using arrays, you may want to use the vector class because it's STL.

and maybe you want to modify that a little bit, to take in data_array[256][2]
and perhaps double parse, because you need to parse it by the '=' character as well.

Share on other sites
Quote:
 Original post by Tradonebut as the title suggests, can you help with STL string class not boost::
IMO suggesting the use of boost, where applicable, is a reasonable answer to any general C++ query. If someone asks for help implementing an array class, should you not recommend std::vector<>? If someone is struggling with character pointers, should you not suggest the use of std::string? I agree that rolling your own can be a good exercise, but otherwise I guess I don't get your point.

Share on other sites
Quote:
 but as the title suggests, can you help with STL string classnot boost::

It's already been established that std::string doesn't have this functionality built in, so we need to use other code.

boost is a good suggestion for this case.

Share on other sites
Quote:
Original post by jyk
Quote:
 Original post by Tradonebut as the title suggests, can you help with STL string class not boost::
IMO suggesting the use of boost, where applicable, is a reasonable answer to any general C++ query. If someone asks for help implementing an array class, should you not recommend std::vector<>? If someone is struggling with character pointers, should you not suggest the use of std::string? I agree that rolling your own can be a good exercise, but otherwise I guess I don't get your point.

yes, you are truely correct.
however, what I am trying to say is 'we still haven't answered the question ASKED'

Share on other sites
hi eveyone, thanks for the quick and awesome reponses. after reviewing my design with a really smart person, i was able to tone down the algorithm and making it much simplier.

thanks and i really appreciate all the responses!

strtok()....

Share on other sites
strtok is an evil function. It mangles the string you pass it and it will explode in a multi threaded program.

Share on other sites
Quote:
 Original post by BeerNuttsstrtok()....
Do you have any particular reason for recommending strtok() over the aforementioned boost::tokenizer<>?

Share on other sites

This topic is 4305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628710
• Total Posts
2984334

• 23
• 11
• 10
• 13
• 14