charIndex property

Paul Dupuis paul at
Thu Jul 27 16:22:21 EDT 2023

On 7/27/2023 4:31 AM, Mark Waddingham via use-livecode wrote:
> On 2023-07-26 18:02, Paul Dupuis via use-livecode wrote:
>> If I have some text in a field, I can use the "charIndex" property 
>> (see Dictionary) to obtain teh character position of the first 
>> character of a chunk.
>> Does anyone know of a clever way to do the equivalent of the 
>> charIndex for an arbitrary chunk expression for a container/variable 
>> (i.e. not an actual field object)?
> This should work I think:
>    function charIndexOfWord pWordIndex, pTarget
>       delete word pWordIndex to -1 of pTarget
>       return the number of characters in pTarget + 1
>    end charIndexOfWord
> Deletion of chunks works from the first char that makes up the 
> computed range, so you are left with all the characters which sit 
> before it.
> The index of the character immediately before the start of the 
> specified word is the just the number of characters which sit before 
> it; and so the index of the first char of the specified word (which is 
> what charIndex gives you in a field) is that +1.
> The above should work for both +ve and -ve indices, and the obvious 
> changes will make it work for other string chunks (i.e. change 'Word' 
> for <chunk>).


Thank you very much. This was a brilliant approach and I should have 
thought of it myself. However, it is not quite an accurate substitute 
for the charIndex property of a field. The following example illustrates 
the issue:

pTarget is [The quick brown fox jumps over the lazy dog. The lazy dog 
was named "Oz".]
pWordIndex is 8 (having been derived from searching for 'lazy', the 8th 

Using [] to quote strings.
delete word 8 to -1 of pTarget -- deletes [lazy] to ["Oz"] but not the 
period (.) at the end since it is not considered part of word -1.
This leaves pTarget as [The quick brown fox jumps over the .]
The number of characters in pTarget + 1 is actually not the position of 
the [l] in [lazy], which is character 36, but the [a] in [azy], 
character 37, due to the period being left.

There are some similar issues, being off by  or more, with sentences and 
paragraphs in longer text.

Thank you very much for chiming in with a good direction to try.

Paul Dupuis

More information about the use-livecode mailing list