Am 04.04.2022 um 03:24 schrieb Allin Cottrell:
On Sun, 3 Apr 2022, Allin Cottrell wrote:
> There isn't a built-in function that gives you exactly the match
> position of one string in another, but the strstr() function can be
> used for that purpose, as in
>
> <hansl>
> string s = "What's in a name?"
In the spirit of Jack's famous one-liners I was going to suggest the
following: strlen(strsplit(s, "name", 1))
However, this gives me 11 instead of the expected 12. And the reason is
that trailing whitespace is chopped off by strsplit, as is duly documented.
So the one-liner based on Allin's suggestion is:
strlen(s) - strlen(strstr(s, "name"))
or in general strlen(s) - strlen(strstr(s, tobefound)) with tobefound
properly defined. I don't think the prior check s != "" is necessary.
Actually we could put this into the 'extra' addon; apparently there is
some demand.
But note that this is just based on the first occurrence that is found.
In principle we could generalize this to return a matrix with all positions.
cheers
sven