Finding A Character (.) In A String - Return Position
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Finding A Character (.) In A String - Return Position
Hi Guys!
I am trying to locate a "." in a string and return its character position within the string.
Simple I know, but the Dictionary has been of no help. Thanks for any and all replies!
Googie.
I am trying to locate a "." in a string and return its character position within the string.
Simple I know, but the Dictionary has been of no help. Thanks for any and all replies!
Googie.
-
- Posts: 720
- Joined: Thu Sep 11, 2014 1:49 pm
- Location: The Netherlands
-
- Livecode Opensource Backer
- Posts: 9385
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Finding A Character (.) In A String - Return Position
Garden spade time:
-
Code: Select all
on mouseUp
ask "Enter your string here: "
put it into SSTRING
if SSTRING contains "." then
put it into SSTRINGY
put 0 into POZ
repeat until char 1 of SSTRING is "."
add 1 to POZ
delete char 1 of SSTRING
end repeat
put " '.' is char #" & POZ && "of string '" & SSTRINGY & "'" into fld "fOUTPUT"
else
put "Your string does not contain a '.' " into fld "fOUTPUT"
end if
end mouseUp
- Attachments
-
- Ball of string.livecode.zip
- Here's the stack.
- (1.08 KiB) Downloaded 204 times
-
- VIP Livecode Opensource Backer
- Posts: 7235
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Finding A Character (.) In A String - Return Position
That's way too much work.Garden spade time:
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
-
- Livecode Opensource Backer
- Posts: 9385
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Finding A Character (.) In A String - Return Position
Yup, so it seems, but I got a bit "offset" by domestic problems here.
And "too much work" is all a bit relative as that took me no longer than 10 minutes.
-
- VIP Livecode Opensource Backer
- Posts: 7235
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Finding A Character (.) In A String - Return Position
That's okay, the whole world has domestic problems right now.Yup, so it seems, but I got a bit "offset" by domestic problems here.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
-
- Livecode Opensource Backer
- Posts: 9385
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Finding A Character (.) In A String - Return Position
I'm not entirely convinced that this is advantageous in any way:
-
-
Of course it might not be a bad thing if
a script could locate ALL the occurrences of "." in a paragraph . . .
Code: Select all
on mouseUp
ask "Enter your string here: "
put it into SSTRING
if SSTRING contains "." then
put offset(".", SSTRING) into POZ
put " '.' is char #" & POZ && "of string '" & SSTRING & "'" into fld "fOUTPUT"
else
put "Your string does not contain a '.' " into fld "fOUTPUT"
end if
end mouseUp
Of course it might not be a bad thing if
a script could locate ALL the occurrences of "." in a paragraph . . .
- Attachments
-
- Ball of string 2.livecode.zip
- Here's the stack.
- (1.06 KiB) Downloaded 212 times
-
- VIP Livecode Opensource Backer
- Posts: 7235
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Finding A Character (.) In A String - Return Position
Using offset, you don't need to check whether the text contains the character, so the "if" clause is unnecessary. If the character isn't in the text, offset returns zero. In your example the script needs to scan the text twice, once to check for the character and again to determine its offset.
You can find all occurances of a character by using offset's optional third parameter. This is the equivalent of "send in time" and is very fast because it only needs to go through the text once. It does require a few more lines of script, where a single offset search only needs one line.
You can find all occurances of a character by using offset's optional third parameter. This is the equivalent of "send in time" and is very fast because it only needs to go through the text once. It does require a few more lines of script, where a single offset search only needs one line.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: Finding A Character (.) In A String - Return Position
Mind posting an example, Jacque? Offset parameters 1 and 2 I think I have a fair understanding of, but I don't see how that 3rd paramter (relative position) would help me to find all of the occurrences of a character (or string of chars)jacque wrote: ↑Fri Jun 26, 2020 6:01 pmYou can find all occurances of a character by using offset's optional third parameter. This is the equivalent of "send in time" and is very fast because it only needs to go through the text once. It does require a few more lines of script, where a single offset search only needs one line.
-
- Livecode Opensource Backer
- Posts: 9385
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Finding A Character (.) In A String - Return Position
Code: Select all
on mouseUp
put empty into fld "fOUTPUT"
put fld "fINPUT" into SSTRING
put the number of chars in SSTRING into TLIMIT
put 0 into SKIPPY
put 1 into TOCHKA
repeat until (SKIPPY + 1) > (TLIMIT)
put offset(".", SSTRING, SKIPPY) into POZ
put " '.' is char #" & (POZ + SKIPPY) && "of string '" & SSTRING & "'" into line TOCHKA of fld "fOUTPUT"
add POZ to SKIPPY
add 1 to TOCHKA
end repeat
end mouseUp
- Attachments
-
- Ball of string 3.livecode.zip
- Here's the stack.
- (1.27 KiB) Downloaded 202 times
-
- Livecode Opensource Backer
- Posts: 9385
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Finding A Character (.) In A String - Return Position
You can, but it is a "right fudge" doing it:You can find all occurrences of a character by using offset's optional third parameter.
Code: Select all
put offset(".", SSTRING, SKIPPY) into POZ
put " '.' is char #" & (POZ + SKIPPY) && "of string '" & SSTRING & "'" into line TOCHKA of fld "fOUTPUT"
add POZ to SKIPPY
As you can see the third optional parameter is a real so-and-so as it does NOT refer to a second or third occurrence,
it refers to the number of characters from the start of the string that have to be excluded during the operation
[It took me 2 glasses of whisky, a hot bath and a completely irrelevant argument with my wife to work that out.]
The 'Documentation' "leaves a bit to be desired" in this respect.
-
- VIP Livecode Opensource Backer
- Posts: 7235
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Finding A Character (.) In A String - Return Position
Right, the third parameter is always the number of characters from the start of the string, which does mean you need to add the most current found position to the counter. It's a bit confusing because offset returns the position from the last starting point, which is the end of the last found position.
The MC docs had a reminder to always add the two locations together. The LC docs would benefit from the same.
The MC docs had a reminder to always add the two locations together. The LC docs would benefit from the same.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com