Re: [GLib] How to get substrings?



On Tue, 2004-11-09 at 09:06 -0800, Alan M. Evans wrote:
Isn't that a bit much? How about...

You probably would want to move pos1 on the length of needle1  before
looking for needle2 first, otherwise if you look for something between
"$" and "$" pos2 will be the same as pos1.

E.g.:

gchar*
substring_between2(gchar* haystack, gchar* needle1, gchar* needle2)
{
    gchar* pos1;
    gchar* pos2;

    if( haystack == NULL || needle1 == NULL || needle2 == NULL )
        return NULL;
    pos1 = g_strrstr( (const gchar*) haystack,
                       (const gchar*) needle1 );
    if( pos1 == NULL )
        return NULL;

      pos1 += strlen (needle1);

    pos2 =  g_strrstr( (const gchar*) pos1,
                       (const gchar*) needle2);
    if( pos2 == NULL )
        return NULL;
    return g_strndup(pos1, pos2-pos1);
}

-- 
Regards,
Martyn



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]