charIndex property
Bob Sneidar
bobsneidar at iotecdigital.com
Mon Jul 31 12:11:51 EDT 2023
On Jul 31, 2023, at 8:54 AM, Paul Dupuis via use-livecode <use-livecode at lists.runrev.com> wrote:
Bob,
Here is a version of Mark's method, for trueWords, sentences, and paragraphs, with the added parameter of pDirection to get the char index of the start of the chunk or the end of the chunk containing the character position pChunkIndex.
Thanks, but whatever email app you sent from borked the code, putting asterisks before and after every keyword and running other words together. I always paste/copy my code in a generic text editor before sending to the list.
At any rate, I cleaned it up and here is the code. Not sure why you made it a private function but I removed the keyword PRIVATE so it could be used anywhere.
Thanks Mark for this effort. I think it should become part of the Master Library.
Bob S
function rwCharIndex pText, pChunkType, pChunkIndex, pDirection
-- pText is the full text
-- pChunkType is once of: words|sentences|paragraphs
-- pChunkIndex is the integer index in the indicated units. ie. "word",7 is the 7th word
-- pDirection is one of: first|last meaning either the 1st character of the chunk or the last character
-- error checking, empty is returned if an error occurs with the parameters
if pText is empty then return empty
if pChunkType is not among the items of "words,sentences,paragraphs" then return empty
if pChunkIndex is not an integer then return empty
if pDirection is not among the items of "first,last" then return empty
local tL
switch pChunkType
case "words"
switch pDirection
case "first"
put null into trueWord pChunkIndex to-1 of pText
put codeunitOffset(null,pText) into N
delete codeunit N to-1 of pText
return (the number of chars in pText + 1)
break
case "last"
put length(trueWord pChunkIndex of pText) into tL
put null into trueWord pChunkIndex to-1 of pText
put codeunitOffset(null,pText) into N
delete codeunit N to-1 of pText
return (the number of characters in pText + tL)
break
end switch
break
case "sentences"
switch pDirection
case "first"
put null into sentence pChunkIndex to-1 of pText
put codeunitOffset(null,pText) into N
delete codeunit N to-1 of pText
return (the number of chars in pText + 1)
break
case "last"
put length(sentence pChunkIndex of pText) into tL
put null into sentence pChunkIndex to-1 of pText
put codeunitOffset(null,pText) into N
delete codeunit N to-1 of pText
return (the number of characters in pText + tL)
break
end switch
break
case "paragraphs"
switch pDirection
case "first"
put null into paragraph pChunkIndex to-1 of pText
put codeunitOffset(null,pText) into N
delete codeunit N to-1 of pText
return (the number of chars in pText + 1)
break
case "last"
put length(paragraph pChunkIndex of pText) into tL
put null into paragraph pChunkIndex to-1 of pText
put codeunitOffset(null,pText) into N
delete codeunit N to-1 of pText
return (the number of characters in pText + tL)
break
end switch
break
end switch
end rwCharIndex
More information about the use-livecode
mailing list