[DGD] SubStr

Michael McKiel crashnbrn71 at yahoo.ca
Wed Mar 24 00:01:44 CET 2004


Figure I'll just post the code, not that its anything spectactular but it is
one of my most used functions in my string.c 

/*	(str) Substring of <s>, starting at position <i>, with length <j>.
 *	If <j> is omitted, it defaults to the remaining length of <s>.  If
 *	<i> or <j> is negative, they are counted as absolute values from the
 *	end of <s>. */
string substr (string str, int start, varargs int end)
{
    int sz;
    sz = strlen(str) -1;

    if (start < 0) {
        start = sz + start + 1;
        if (start < 0) start = 0;
    }
	else if (start > sz)
		return nil;

    if (end >= 1) {
        end += start -1;
        if (end > sz) end = sz;
    }
	else {
        end = sz + end;
        if (end < 0 || end < start)
            return nil;
    }
    return str[start..end];
}


______________________________________________________________________ 
Post your free ad now! http://personals.yahoo.ca
_________________________________________________________________
List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list