charIndex property

Mark Waddingham mark at livecode.com
Thu Jul 27 04:31:28 EDT 2023


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>).

Hope this helps!

Warmest Regards,

Mark.

-- 
Mark Waddingham ~ mark at livecode.com ~ http://www.livecode.com/
LiveCode: Build Amazing Things



More information about the use-livecode mailing list