Skip to main content
gamedev.net
🔒 Locked

split a string with string.h

Started by nick5454 May 8, 2003 at 10:08 PM 12 replies 850+ views
Original Post
nick5454
nick5454
Does string.h have a function much like split in VB. By that I mean I have a comma delimited string. Does string.h have one that will split into a char array od some sort?
Sneftel
Sneftel
yeah, there''s strtok. But it kind of really sucks. In C++, you can use stringstreams instead, which are more useful and flexible.


How appropriate. You fight like a cow.
nick5454
nick5454
is.getline(s1,100,'' '');
temps1 = (char*)memchr(s1,'','',sizeof(s1));

if ( temps1 != NULL )
count << temps1-s1+1

I got that code which gives me the end location

s1 is the string ( char *)
temps1 is the ptr to the comma

so I want the first 3 chars like say 31 to 34 in memory, is there a function that will create a char* from those locations?
nick5454
nick5454
i guess like the mid function in basic
nick5454
nick5454
or do i write my own little function
nick5454
nick5454
maybe memccpy but not sure
Sneftel
Sneftel
strtok. stringstream. boost::tokenizer. Use one of those.


How appropriate. You fight like a cow.
Ishan
Ishan
quote:
Original post by nick5454
is.getline(s1,100,' ');
temps1 = (char*)memchr(s1,',',sizeof(s1));

if ( temps1 != NULL )
count << temps1-s1+1

I got that code which gives me the end location

s1 is the string ( char *)
temps1 is the ptr to the comma

so I want the first 3 chars like say 31 to 34 in memory, is there a function that will create a char* from those locations?



  

#include <vector>
using std::vector;

vector<string> strchop(string s,char delim = ','){
unsigned int i = 0;
vector<string> ret;
string tmp;

while(i < s.size()){
if(s[i] == delim){
ret.push_back(tmp);
tmp = "";
} else tmp += s[i];
i++;
}
if(tmp != "
") ret.push_back(tmp);
return ret;
}



This seems to be the most elegant solution that comes to mind. Just run strchop("i,like,cookies!!") and you'll get the appropriate vector.





[edited by - Ishan on May 9, 2003 2:42:19 PM]
daerid
daerid
wow.. nice code. I''ve been beating myself up using string::find and string::substr for way too long.

Thanks!
daerid@gmail.com
nick5454
nick5454
so what would be faster using memcpy with char* or the vector?
antareus
antareus
quote:
Original post by nick5454
so what would be faster using memcpy with char* or the vector?

memcpy with char* unless you really mess up.

You should be asking: what would be clearer ? We''re talking about splitting hairs here when it comes to execution efficiency.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
nick5454
nick5454
you aint kidding, i''m splitting them right now
antareus
antareus
May I ask what you are writing that sacrifices readability and type safety for speed?
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
nick5454
nick5454
nah, I just wanted to do it the hard way so I understood it before i used the std::string thats all

Topic Locked

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

Sign in to reply to this topic.