How to get word offset all instances of a string in a chunk of text?

Jim Lambert jiml at netrin.com
Thu Aug 30 18:20:01 EDT 2018


> On 30/08/2018 10:24, Keith Clarke via use-livecode wrote:
>> Folks,
>> Is there a single-pass mechanism or more efficient way of returning the wordOffset of each instance of ?the? in ?the quick brown fox jumped over the lazy dog? than to use two passes through the text?

Then there is also this repeat-less approach using arrays and filter:

function findWordOffsets pText, pSearchTerm
	put replaceText(pText,"\W+"," ") into pText
	split pText by space
	combine pText with cr and tab
	filter pText with "*" & tab & pSearchTerm
	sort numeric pText
	return pText
end findWordOffsets

put "Then the quick brown fox jumped over "The" very,

very lazy

red dog on the sofa.” into temp       — note the extra spaces and line breaks.

put findWordOffsets(temp, “the”)   
returns:
2	the
8	The
15	the

Jim Lambert






More information about the use-livecode mailing list