Split a string into pieces based on a delimiter.
split-string will find all instances of <delimiter> in <string> and then split it into pieces. The <result> is a pointer to a variable of type
"vely_split_str" and it has two members: integer variable
"num_pieces" which is the number of pieces <string> was broken into, and string array
"pieces", which holds the actual pieces, each null-terminated. If variable <result> does not exist, you can create it with define clause. A delimiter within double quotes (
"..") is not counted, i.e. it is skipped.
Note that <string> is altered by placement of null-terminators and will not hold the same value (rather it will hold only the leading portion of what it did before split-string took place). Each element of
"pieces" array points to memory occupied by <string>. Hence, split-string does not copy any data and is very fast in performing the kind of parsing described here. You can copy string beforehand if you don't want it altered (see
copy-string).
All pieces produced will be trimmed both on left and right. If a piece is double quoted, then double quotes are removed. For instance:
char clist[] = "a , b, \"c , d\" , e"
split-string clist with "," to define res
After this, the variable
"res" will be an array of strings with these values:
res->num_pieces is 4
res->pieces[0] points to "a"
res->pieces[1] points to "b"
res->pieces[2] points to "c , d"
res->pieces[3] points to "e"
Also, since <string> is altered, it cannot be a constant - rather it must always be a variable, for example, if you do this with the intention to split this string based on
"," as a delimiter:
char *str = "string,to,split";
your program will report an error (SIGSEGV most likely, or segmentation fault). You should do:
char str[] = "string,to,split";
split-string is useful for parsing CSV (Comma Separated Values) or any other kind of separated values, where separator can be any string of any length, for example if you're parsing an encoded URL-string, then
"&" may be a separator, as in the example below.
If
"delete" clause is used, memory allocated for <result> (in a previously executed split-string) is released.