- Subject: Re: [slang-users] Multi-character delimiter for strchop()?
- From: jed@xxxxxxxxxxx (John E. Davis)
- Date: Sun, 5 Oct 2014 17:00:34 -0400
J.B. Nicholson-Owens <jbn@xxxxxxxxxxxxxxx> wrote:
> Would it be possible and appropriate to extend strchop() to allow for a
> string to be used as a delimiter to chop other strings?
>
> So strchop("afoobfooc", "foo", 0) would return the string array
> ["a","b","c"]?
Will this function work?
define strchop2 (str, delim)
{
variable list = {};
variable len = strbytelen (delim);
variable i0 = 1, i;
while (i = is_substrbytes (str, delim, i0), i != 0)
{
list_append (list, substrbytes (str, i0, i-i0));
i0 = i + len;
}
list_append (list, substrbytes (str, i0, -1));
return list_to_array (list);
}
Here is a driver for it:
private define aprint (a)
{
print (_reshape (a, [1, length(a)]));
}
define slsh_main ()
{
aprint (strchop2 ("foo", "foo"));
aprint (strchop2 ("foofoo", "foo"));
aprint (strchop2 ("afoo", "foo"));
aprint (strchop2 ("fooa", "foo"));
aprint (strchop2 ("afoob", "foo"));
aprint (strchop2 ("afoobfooc", "foo"));
aprint (strchop2 ("afoofoo", "foo"));
aprint (strchop2 ("fooafoo", "foo"));
aprint (strchop2 ("foofooa", "foo"));
aprint (strchop2 ("abc", "foo"));
aprint (strchop2 ("ab", "foo"));
aprint (strchop2 ("a", "foo"));
aprint (strchop2 ("", "foo"));
}
It produces:
"" ""
"" "" ""
"a" ""
"" "a"
"a" "b"
"a" "b" "c"
"a" "" ""
"" "a" ""
"" "" "a"
"abc"
"ab"
"a"
""
Thanks,
--John
_______________________________________________
For list information, visit <http://jedsoft.org/slang/mailinglists.html>.
[2014 date index]
[2014 thread index]
[Thread Prev] [Thread Next]
[Date Prev] [Date Next]