How to Change All Occurrences of a Substring to Uppercase in a Field
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
How to Change All Occurrences of a Substring to Uppercase in a Field
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!
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
I was overcomplicating things. I believe I’ve found the most straightforward way to do it.
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'.
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
-
- 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
Well . . .
. . . how about searching for SPACE + string + SPACE?
. . . how about searching for SPACE + string + SPACE?
Re: How to Change All Occurrences of a Substring to Uppercase in a Field
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
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"
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
Try this:
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
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
Craig
Re: How to Change All Occurrences of a Substring to Uppercase in a Field
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:
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
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
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...