From owner-freebsd-questions@FreeBSD.ORG Wed Sep 17 13:32:57 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 54E001065672 for ; Wed, 17 Sep 2008 13:32:57 +0000 (UTC) (envelope-from unga888@yahoo.com) Received: from web57005.mail.re3.yahoo.com (web57005.mail.re3.yahoo.com [66.196.97.109]) by mx1.freebsd.org (Postfix) with SMTP id F0A478FC53 for ; Wed, 17 Sep 2008 13:32:56 +0000 (UTC) (envelope-from unga888@yahoo.com) Received: (qmail 61786 invoked by uid 60001); 17 Sep 2008 13:32:56 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Mailer:Date:From:Reply-To:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Message-ID; b=oOBR3P1l3KBDb0fLYtSe82qnv4JcxFOu/E7DD8XblzjD5HIHlW3tIMJxTXWmVorDJVb3yfGk/v/K5ley8UX70E1vxjDSTtNK9znGIVhvQ7N3m9+vPD6MQd/wHdBMNR1xaGGvAGo+7H6NveARggaZ/+hJdPJ0l/3QpKZDVf3iL6w=; X-YMail-OSG: lwEvKFsVM1lu1J_xf9.MLmsR2zgravnZohgVBxfkrC5e_L1jz25KhO8gtToSkjsyCGMVHX.YpguC_X3PG7SWWstSGe2on_ysIg_iuTapDFVD1g_h3oO8_xEPqFtJhNS_QxSpn21m2RjunVajEomGfy2P.E8VlfA1rMKGDMG7PjoXZ1TSmMFIQbJoHqWkqiPLKNb6fEf0zLQE_mbx7S7xf1vq2pwx Received: from [220.255.7.144] by web57005.mail.re3.yahoo.com via HTTP; Wed, 17 Sep 2008 06:32:56 PDT X-Mailer: YahooMailWebService/0.7.218.2 Date: Wed, 17 Sep 2008 06:32:56 -0700 (PDT) From: Unga To: Giorgos Keramidas In-Reply-To: <87wshboz40.fsf@kobe.laptop> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Message-ID: <417898.61773.qm@web57005.mail.re3.yahoo.com> Cc: freebsd-questions@freebsd.org Subject: Re: How to split a C string by a string? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: unga888@yahoo.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 17 Sep 2008 13:32:57 -0000 --- On Wed, 9/17/08, Giorgos Keramidas wrote: > From: Giorgos Keramidas > 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 00:45:46 -0700 (PDT), Unga > wrote: > > Hi all > > > > I'm writing an C application on FreeBSD 7+. I need > to split a string > > by another string (ie. the delimiter is > "xxx") similar to strtok split > > a string by a single char. Is there a standard > function or is there a > > FreeBSD functions for this? > > You can use strstr() to look for the "xxx" > delimited and split that that > point: > > % cat -n foo.c > 1 #include > 2 #include > 3 > 4 int > 5 main(void) > 6 { > 7 char text[] = "Hello string > world"; > 8 char delim[] = " string "; > 9 size_t dlen = sizeof(delim) / > sizeof(delim[0]) - 1; > 10 char *p; > 11 > 12 p = strstr(text, delim); > 13 if (p == NULL) > 14 return 0; /* No > match */ > 15 > 16 printf("First part = > \"%.*s\"\n", p - text, text); > 17 printf("Second part = > \"%s\"\n", p + dlen); > 18 return 0; > 19 } > % cc -std=iso9899:1990 -O2 -Wall foo.c > % ./a.out > First part = "Hello" > Second part = "world" > % Thank you very much for the reply. That is, there is no existing split function. So I got to write to my own :) Best regards Unga