Advertisement

Split a string into an array???

Started by March 13, 2001 11:51 AM
3 comments, last by BigReaper 23 years, 10 months ago
Hello there!! Is there any way to split a string into an array by a given seperator, like the "|", in C/C++ ? Perhaps you know the function from VB or TCL ... there it is called "Split". I really miss that function in C/C++, because it makes developing a scripting-engine very much easier. hope anybody can help me, BigReaper
I don''t think there''s a function that will do that whole thing for you, but it''s pretty easy to write your own, and just drop it into a library of your most commonly used routines. Look up the strstr() function for locating the separator within the string, and it''s fairly easy from there.

-Ironblayde
 Aeon Software

Down with Tiberia!
"All your women are belong to me." - Nekrophidius
"Your superior intellect is no match for our puny weapons!"
Advertisement
Yes, but how can I split a string into two strings to add them to an array? and is there any chance to return an array from a function?
Sorry about these newbie questions, but i AM quite a newbie
There is a function strtok (char *strToken, const char *strDelimit) in C++.

It is in string.h

parameters:
strToken - String containing the delimiting tokens token(s)
strDelimit - Set of delimiter characters

Calling strtok gives the string before the first delimiter. Every successive call of strtok with strToken as NULL, gives the successive delimiters.

Below is the example of how you use it

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include
#include

char string[] = "This|is|a|Test|string";

// Now set the delimiter as "|"
char seps[] = " |";
char *token;

void main( void )
{
printf( "%s\n\nTokens:\n", string );

/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );

/* Get next token: */
token = strtok( NULL, seps );
}
}


Output

This|is|a|Test|string

Tokens:
This
is
a
Test
string

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

strtok is a pretty powerful function. You can even give a set a delimters, instead of a single delimiter as given in the example below. The function retrieves the token when any one of the delimters in the delimiter string is encountered.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include
#include

char string[] = "A string\tof ,,tokens\nand some more tokens";

// The statement below implies that the delimiter can be one
// of the characters in seps[] array.
// Here is " " or "," or "\t" or "\n".
char seps[] = " ,\t\n";
char *token;

void main( void )
{
printf( "%s\n\nTokens:\n", string );

/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );

/* Get next token: */
token = strtok( NULL, seps );
}
}


Output

A string of ,,tokens
and some more tokens

Tokens:
A
string
of
tokens
and
some
more
tokens

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

If you want more information, refers to MSDN or at the website "http://msdn.microsoft.com/library/default.asp" and search for strtok.

Hope this is what you require.

Arun



YAAAAAAAAAHHHHUUUUUUUUUUUU!!!!!!!!!
THANX VERY MUCH !!!
this is exactly the function i was looking for !!
THANX THANX THANX THANX THANX THANX THANX THANX THANX

This topic is closed to new replies.

Advertisement