[Gretl-devel] Suggestion for new string-checking built-in function
cottrell at wfu.edu
Fri Aug 19 10:27:50 EDT 2016
On Fri, 19 Aug 2016, Sven Schreiber wrote:
> it often occurs (at least in my own and in Artur's hansl programming) that
> you want to check whether a certain string variable contains some shorter
> string. For example whether variable 's1' contains the string "USA", without
> being necessarily exactly equal to "USA".
> Currently to my knowledge this can/must be done using the strstr() function,
> either like this (producing non-negative integer output):
> strlen(strstr(s1, "USA"))
> or like this:
> strstr(s1, "USA") != ""
> I find both variants relatively clumsy, so I'm asking for syntactic sugar
> which would be equivalent to the second expression above. For example an
> 'instring()' function that would be loosely analogous to the inbundle() or
> inlist() functions.
> So the above example would simply be rewritten as:
> instring(s1, "USA")
> But maybe Jack or somebody else have an alternative suggestions what could be
> done instead with existing hansl constructs.
How about if we emulated C (sort of) and allowed empty and non-empty
strings to evaluate as 0 and 1 where the context requires a Boolean
result? Then you could just say
if strstr(s1, "USA")
# something else
(Strictly, in C "strstr(s1, s2)" in a Boolean context is equivalent to
"strstr(s1, s2) != NULL". But we don't have truly NULL strings
floating around in hansl, and emptiness seems like the moral
More information about the Gretl-devel