Page 1 of 1

How to Change All Occurrences of a Substring to Uppercase in a Field

Posted: Fri Sep 20, 2024 8:01 am
by Fermin
Hi,
I need help converting all occurrences of a specific substring to uppercase within a field. I’m already using the upper function to convert to uppercase, but my main issue is finding all occurrences of the substring and putting them back in the same position. Is it possible to do this using the offset function? What’s the most efficient way to achieve this in LiveCode?

Thanks!

Re: How to Change All Occurrences of a Substring to Uppercase in a Field

Posted: Fri Sep 20, 2024 9:13 am
by Fermin
I was overcomplicating things. I believe I’ve found the most straightforward way to do it.

Code: Select all

on mouseUp
   put "in front" into obje
   put upper(obje) into obje2
   replace obje with obje2 in field almacenb preserving styles
end mouseUp
mmm... Although I hadn’t considered the problem of finding a substring within another word, what I need is for the search to find whole words only. For example, if I want to replace all instances of 'here' with 'HERE', I don’t want 'there' to become 'tHERE'.

Re: How to Change All Occurrences of a Substring to Uppercase in a Field

Posted: Fri Sep 20, 2024 1:19 pm
by richmond62
Well . . .

. . . how about searching for SPACE + string + SPACE?

Re: How to Change All Occurrences of a Substring to Uppercase in a Field

Posted: Fri Sep 20, 2024 5:36 pm
by Fermin
Thank you very much, Richmond, but I'm not sure if it would work when the target is at the beginning of a line or after a period or a comma... Also, the algorithm would need to work both for a single word (e.g., "here") and for a small phrase (e.g., "in front").

Re: How to Change All Occurrences of a Substring to Uppercase in a Field

Posted: Fri Sep 20, 2024 8:27 pm
by emiclo
A start:

on mouseup
put "some text" into searchedString
put the length of searchedString into stringLength
put zero into codepointsToSkip
put zero into startCharOfSearchedString
put "no" into endOfSearch

repeat until endOfSearch is "yes"

put codepointoffset(searchedString, field "my field", codepointsToSkip) into startCharOfSearchedString

if startCharOfSearchedString is zero then
put "yes" into endOfSearch
else
put toUpper(searchedString) into char startCharOfSearchedString to (startCharOfSearchedString + stringLength - 1) of field "my field"
add stringLength to codepointsToSkip
end if

end repeat

end mouseup

problem with "some texts"

Re: How to Change All Occurrences of a Substring to Uppercase in a Field

Posted: Fri Sep 20, 2024 10:53 pm
by dunbarx
Try this:

Code: Select all

on mouseUp

   ask "Find?"
   repeat 100 --arbitrary
      find it in fld 1
      put the foundChunk & return after temp
   end repeat
      
   repeat with y = 1 to the number of lines of temp
      put toUpper(it) into char (word 2 of line y of temp) to (word 4 of line y of temp)\
            of fld 1
   end repeat
end mouseUp
The "repeat 100" is arbitrary, just because I am lazy. The right way to do it is to stop when the foundChunk is repeated the first time. If less lazy yet, one would not have to build a list of foundChunks, but would make the substitution on the fly.

Craig

Re: How to Change All Occurrences of a Substring to Uppercase in a Field

Posted: Fri Sep 20, 2024 10:57 pm
by dunbarx
This method was because way back in the day, I recalled that repeated "finds" in Hypercard automatically advanced through the text of interest to the next occurrence. Same with LC, apparently. Not sure if that is documented...

Craig

EDIT; Yep, the dictionary says:
The find command starts searching after the previously-found text (if there was a previous find command) or at the beginning of the first field on the current card (if not).

Re: How to Change All Occurrences of a Substring to Uppercase in a Field

Posted: Sat Sep 21, 2024 5:48 pm
by stam
Fermin wrote:
Fri Sep 20, 2024 9:13 am
I want to replace all instances of 'here' with 'HERE', I don’t want 'there' to become 'tHERE'.
if you just want to replace a word with upper case of the same, you can do this in one line using regex, where in the code below pSource is the container containing the source text and pText contains the word you wish to make upper case:

Code: Select all

put replaceText(pSource, "(?mi)(?<=\s|^)(" & pText & ")(?=s|$|,|\.|_)", toUpper(pText)) into pSource
I have not tested this extensively, but it seems to work.

This uses positive lookbehind (?<=...) and positive lookahead (?=...) to confirm the text being replaced is either preceded by start of a sentence, a space and if followed by a space, comma, full stop or underscore, which you can tailor if needed. It replaces all instances.

Let me know if the regex doesn't make sense...