Skip to main content
GameDev.net gamedev.net
🔒 Locked

Visual Basic-style string splitting?

Started by storage Mar 10, 2005 at 2:24 PM 5 replies 1k views
Original Post
storage
storage
Hello! I've been trying over and over again to find a way to split a string into an array of strings. I have tried Google, writing the code myself, etc but without any success. I want something like this, so that the array holds every word of a string:

char *message = "this is a test";
char *words[];

words = splitstring(message, " ");

Is this possible? Thanks in advance, storage!
MENTAL
MENTAL
You were asking for that :).
daerid
daerid
I would look into c++ and use Boost.StringAlgorithm
daerid@gmail.com
Eps
Eps
What you are curious about is called string delimination. It is really a rather simple algorithm. Start at the beginning, and go through the array of characters until the character you wish to deliminate by is found. Allocate the memory for 0 to N and then strncpy into the new array for length N. Now record where N was and start the search again. Allocated and strncpy for the new position - N.

Of course, you are going to want to trim the left and right sides of the string of any deliminating characters before you start.

Edit: And in Java it is called tokenizing.
evolutional
evolutional
Oluseyi posted a solution to this using std::string and std::vector once. You can find it here.

Topic Locked

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

Sign in to reply to this topic.