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

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Fermin
Posts: 149
Joined: Fri Jun 05, 2015 10:44 pm

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

Post by Fermin » Fri Sep 20, 2024 8:01 am

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!

Fermin
Posts: 149
Joined: Fri Jun 05, 2015 10:44 pm

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

Post by Fermin » Fri Sep 20, 2024 9:13 am

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10080
Joined: Fri Feb 19, 2010 10:17 am

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

Post by richmond62 » Fri Sep 20, 2024 1:19 pm

Well . . .

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

Fermin
Posts: 149
Joined: Fri Jun 05, 2015 10:44 pm

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

Post by Fermin » Fri Sep 20, 2024 5:36 pm

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

emiclo
Posts: 1
Joined: Mon May 03, 2021 3:07 pm

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

Post by emiclo » Fri Sep 20, 2024 8:27 pm

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"

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » Fri Sep 20, 2024 10:53 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » Fri Sep 20, 2024 10:57 pm

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

stam
Posts: 3061
Joined: Sun Jun 04, 2006 9:39 pm

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

Post by stam » Sat Sep 21, 2024 5:48 pm

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

Post Reply