Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 18 Sep 2008 16:46:00 +1000
From:      "Murray Taylor" <MTaylor@bytecraft.com.au>
To:        "Giorgos Keramidas" <keramida@ceid.upatras.gr>, <unga888@yahoo.com>
Cc:        freebsd-questions@freebsd.org
Subject:   RE: How to split a C string by a string?
Message-ID:  <E194A4DE220BBE4FAF3AB7C4E7EDA086055913@svmailmel.bytecraft.internal>

next in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.

------_=_NextPart_001_01C9195A.3694FC8E
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

> -----Original Message-----
> From: owner-freebsd-questions@freebsd.org
[mailto:owner-freebsd-questions@freebsd.org] On Behalf Of Giorgos
Keramidas
> Sent: Thursday, 18 September 2008 1:49 AM
> To: unga888@yahoo.com
> Cc: freebsd-questions@freebsd.org
> Subject: Re: How to split a C string by a string?
>=20
> On Wed, 17 Sep 2008 06:32:56 -0700 (PDT), Unga <unga888@yahoo.com>
wrote:
> > --- On Wed, 9/17/08, Giorgos Keramidas <keramida@ceid.upatras.gr>
wrote:
> >> From: Giorgos Keramidas <keramida@ceid.upatras.gr>
> >> Subject: Re: How to split a C string by a string?
> >> To: unga888@yahoo.com
> >> Cc: freebsd-questions@freebsd.org
> >> Date: Wednesday, September 17, 2008, 6:17 PM On Wed, 17 Sep 2008=20
> >> 00:45:46 -0700 (PDT), Unga <unga888@yahoo.com> wrote:
> >> > Hi all
> >> >
> >> > I'm writing an C application on FreeBSD 7+. I need to split a=20
> >> > string by another string (ie. the delimiter is "xxx") similar to=20
> >> > strtok split a string by a single char. Is there a standard=20
> >> > function or is there a FreeBSD functions for this?
> >>
> >> You can use strstr() to look for the "xxx" delimited and split that

> >> that point: [snip sample code]>=20
>
> > Thank you very much for the reply. That is, there is no existing
split=20
> > function. So I got to write to my own :)
>=20
> Yes, you have to roll our own.  The standard C library doesn't have=20
> string splitting functions with a string as delimiter.  It includes
strtok(),=20
> strspn() and strcspn(), but these work with character sets as
delimiters, not strings...

Hi,

the attached code does string splitting and insertion

it looks for a specific string, copies from start up to that=20
point to a workspace, inserts the new text, then appends=20
the remainder, starting AFTER the search string fragment.

with a bit of work you could make it do your string splitting....


Murray Taylor
Bytecraft Systems
Special Projects Engineer

P: +61 3 8710 0600
D: +61 3 9238 4275
F: +61 3 9238 4140

--
=20|_|0|_|	"Absence of evidence
=20|_|_|0|	is not evidence of absence"
=20|0|0|0|	Carl Sagan

---------------------------------------------------------------
The information transmitted in this e-mail is for the exclusive
use of the intended addressee and may contain confidential
and/or privileged material. Any review, re-transmission,
dissemination or other use of it, or the taking of any action
in reliance upon this information by persons and/or entities
other than the intended recipient is prohibited. If you
received this in error, please inform the sender and/or
addressee immediately and delete the material.=20

E-mails may not be secure, may contain computer viruses and
may be corrupted in transmission. Please carefully check this
e-mail (and any attachment) accordingly. No warranties are
given and no liability is accepted for any loss or damage
caused by such matters.
---------------------------------------------------------------

### This e-mail message has been scanned for Viruses by Bytecraft ###

------_=_NextPart_001_01C9195A.3694FC8E
Content-Type: application/octet-stream;
	name="test.c"
Content-Transfer-Encoding: base64
Content-Description: test.c
Content-Disposition: attachment;
	filename="test.c"

I2luY2x1ZGUgPHN0ZGlvLmg+DQojaW5jbHVkZSA8c3RkbGliLmg+DQojaW5jbHVkZSA8c3RyaW5n
Lmg+DQoNCg0KY2hhcgkJdGVtcGxhdGUgIFtdID0gIlwNCi8qKioqKioqKioqKioqKioqKioqKioq
KioqKioqKioqKioqKioqKioqKlxuXA0KICogU1RBTkRBUkQgQ0xPU0VEXG5cDQogKi9cblwNClxu
XA0KVG86ICRBRERSJFxuXA0KU3ViamVjdDogUmVxdWVzdFxuXA0KXG5cDQpcInN0YXJ0LXJlcXVl
c3RcIlxuXA0KSm9iIElkOiRKT0JJRCRcblwNClN0YXR1czpDbG9zZWRcblwNClwiZW5kLXJlcXVl
c3RcIlxuXA0KTWVzc2FnZSBzZW50OiAkREFURVRJTUUkXG5cDQpcblwNCi8q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