[Coco] INSTR question
Paulo Garcia
paulo.astuser at gmail.com
Fri Feb 10 15:05:46 EST 2017
Interesting discussion. Indeed the same behaviour is found in Python and
Javascript:
NodeJS:
> a='ABC'
'ABC'
> a.indexOf('A')
0
> a.indexOf('B')
1
> a.indexOf('C')
2
> a.indexOf('')
0
>
Python:
>>> a='ABC'
>>> a.index('B')
1
>>> a.index('A')
0
>>> a.index('')
0
>>>
Paulo
On Fri, Feb 10, 2017 at 2:29 PM, Mathieu Bouchard <matju at artengine.ca>
wrote:
>
> Nope, it's like that in probably every language that has such a search
> function : an empty string is found at EVERY position in the string,
> therefore the first match it finds is wherever the search begins. It's the
> normal way of doing it, because it logically fits the way N characters are
> searched in a string, for N=0, and the behaviour you wish would mean adding
> a special case for N=0 where programmers prefer to define functions so that
> they have the least possible number of cases.
>
> (However, in other languages, 0 is the first position in the string,
> whereas "no match" is represented by another value (such as -1 or nil or
> error))
>
>
> Le 2017-02-09 à 15:12:00, Allen Huffman a écrit :
>
> ...but I noticed today it finds the empty string: ""
>>
>> PRINT INSTR("ABCDE", "")
>> 1
>>
>> That seems like a bug.
>> A$=""
>> PRINT INSTR("ABCD", A$)
>> 1
>>
>
> ______________________________________________________________________
> | Mathieu BOUCHARD --- tél: 514.623.3801, 514.383.3801 --- Montréal, QC
>
>
> --
> Coco mailing list
> Coco at maltedmedia.com
> https://pairlist5.pair.net/mailman/listinfo/coco
>
--
--------------------------------------------
Paulo
More information about the Coco
mailing list