I need an efficient function that extracts first second and rest of the sentence into three variables.
相关问题
- Multiple sockets for clients to connect to
- how to split a list into a given number of sub-lis
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Generate string from integer with arbitrary base i
Easy way: Use strtok() or strtok_r to get the first two tokens, which will remove them from the string, so the string itself will be your third token you were looking for.
Hard way: Parse it yourself :(
Strtok is in the C string library, and will mutate your original string so be careful, copy the string first if it needs to remain intact.
Possible Example:
This should work just fine and be threadsafe with the original string unmutated.
You need to define the delimiters first. There are a few problems with
strtok
(it modifies its argument, for one, which may land you in trouble). I prefer to read in the string and run a custom parser which may range fromsscanf
to a full-blown parser. Please post some more detail.strtok()