Skip to main content
gamedev.net
🔒 Locked

problem with substr

Started by 3dmodelerguy Feb 24, 2005 at 9:48 AM 1 replies 650+ views
Original Post
3dmodelerguy
3dmodelerguy
ok this is the string the i read from a file: graphic_api = d3d; and when i want to take out just the: d3d no spaces and no ';' this is the code i use:

std::string CConfigDatabase::GetValueFromString( const std::string & oringal_string )
{
	// position of start and end of where the value is located in the string
	std::string::size_type position_0 = oringal_string.find_first_of( key_value_delim ) + 1;
	std::string::size_type position_1 = oringal_string.find_last_of( comment_delim );
	// Return the value part of the string, anything to right right of the "=" sign and to the left of the ";" sign
	return oringal_string.substr( position_0, position_1 - position_0 );

but for some reason, this is what is returned: " d3d" i don't know why the space is there. how could i change my code to get that space out? i have tried a -1 and +1 everywhere i can think of but i am for some reason not getting it. the only way to fix it that i can find it to add some code but i would rather not have that, it adds time to process and even tho it is time critcal function i like it to be as fast possiable.
Agony
Agony
Have you tried adding 2 instead of 1? When you search for the '=', you'll get the position of that character. If you used substr() with that value, then the substr's first character would be the '='. Adding 1 starts one character over, and thus starts with the ' '. Adding 2 starts two characters over, thus starting with the 'd'. Though you'll want to make sure that after adding 2 to the position, make sure that position_1 isn't less than position_0, and that neither position_0 nor position_1 are equal to std::string::npos, to be safe.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
hh10k
hh10k
Don't worry about adding code. If it takes you 10 minutes to work out the fastest way, but a standard solution takes only a millisecond longer, then you'll need to run it 600000 times before you make up the time you spent on it :)

If key_value_delim is '=', then you just need to skip the whitespace. This should do the trick:

position_0 = oringal_string.find_first_not_of(' ', position_0)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.