Unix Programming - strtok vs split

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2004 > strtok vs split





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author strtok vs split
John Galt

2004-04-23, 6:34 pm

Is there a C-language equivalent for Java's String.split()?
strtok() kind of sucks: it doesn't quite the same thing, it overwrites
its arg and it keeps track of stuff secretly between call to call.
There has *got* to be a better way. UNIX/C gurus, any ideas?

-John
Måns Rullgård

2004-04-23, 6:35 pm

johngalt__@hotmail.com (John Galt) writes:

> Is there a C-language equivalent for Java's String.split()?
> strtok() kind of sucks: it doesn't quite the same thing, it overwrites
> its arg and it keeps track of stuff secretly between call to call.
> There has *got* to be a better way. UNIX/C gurus, any ideas?


There's always strtok_r() and strsep().

--
Måns Rullgård
mru@kth.se
Dragan Cvetkovic

2004-04-23, 6:35 pm

Måns Rullgård <mru@kth.se> writes:

> johngalt__@hotmail.com (John Galt) writes:
>
>
> There's always strtok_r() and strsep().
>


Strsep?

% man strsep
No manual entry for strsep.

% uname -sr
SunOS 5.10

Bye, Dragan

--
Dragan Cvetkovic,

To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer

!!! Sender/From address is bogus. Use reply-to one !!!
Måns Rullgård

2004-04-23, 7:34 pm

Dragan Cvetkovic <me@privacy.net> writes:

> Måns Rullgård <mru@kth.se> writes:
>
>
> Strsep?
>
> % man strsep
> No manual entry for strsep.
>
> % uname -sr
> SunOS 5.10


Apparently strsep exists only on BSD (and GNU). Get a copy from the
one the BSDs, if you want to use it.

--
Måns Rullgård
mru@kth.se
GVK

2004-04-24, 2:34 am

Dragan Cvetkovic wrote:
> Strsep?
>
> % man strsep
> No manual entry for strsep.
>
> % uname -sr
> SunOS 5.10
>

STRSEP(3) Linux Programmer’s Manual STRSEP(3)

NAME
strsep - extract token from string

SYNOPSIS
#include <string.h>

char *strsep(char **stringp, const char *delim);

DESCRIPTION
If *stringp is NULL, the strsep() function returns NULL and does noth-
ing else. Otherwise, this function finds the first token in the string
*stringp, where tokens are delimited by symbols in the string delim.
This token is terminated with a ‘\0’ character (by overwriting the
delimiter) and *stringp is updated to point past the token. In case no
delimiter was found, the token is taken to be the entire string
*stringp, and *stringp is made NULL.

RETURN VALUE
The strsep() function returns a pointer to the token, that is, it
returns the original value of *stringp.

NOTES
The strsep() function was introduced as a replacement for strtok(),
since the latter cannot handle empty fields. However, strtok() con-
forms to ANSI-C and hence is more portable.

BUGS
This function suffers from the same problems as strtok(). In particu-
lar, it modifies the original string. Avoid it.

CONFORMING TO
BSD 4.4

SEE ALSO
index(3), memchr(3), rindex(3), strchr(3), strpbrk(3), strspn(3),
strstr(3), strtok(3)

GNU 1993-04-12 STRSEP(3)


Ian Zimmerman

2004-04-30, 11:34 pm


John> Is there a C-language equivalent for Java's String.split()?
John> strtok() kind of sucks: it doesn't quite the same thing, it
John> overwrites its arg and it keeps track of stuff secretly between
John> call to call. There has *got* to be a better way. UNIX/C gurus,
John> any ideas?

I had a full C implementation of split(), but threw it away when I got a
new machine. As you can imagine, it was tricky, calling realloc() many
times. Now I think when I _really_ need split (and routines like strspn
won't work) it's time to switch to another language.

--
Nothing can be explained to a stone.
Or to a stoned person, either.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com