Finding A Character (.) In A String - Return Position

The place to discuss anything and everything about running your LiveCode on Android

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
Googie85
Posts: 123
Joined: Tue Aug 05, 2014 10:07 am

Finding A Character (.) In A String - Return Position

Post by Googie85 » Thu Jun 25, 2020 6:44 am

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.

mrcoollion
Posts: 555
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Finding A Character (.) In A String - Return Position

Post by mrcoollion » Thu Jun 25, 2020 6:56 am

Use Offset. How ? See the following URL : https://livecode.fandom.com/wiki/Offset

Regards,

Paul

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4601
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Finding A Character (.) In A String - Return Position

Post by richmond62 » Thu Jun 25, 2020 7:59 am

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
-
Screenshot 2020-06-25 at 9.57.55.png
Attachments
Ball of string.livecode.zip
Here's the stack.
(1.08 KiB) Downloaded 1 time

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5418
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Finding A Character (.) In A String - Return Position

Post by jacque » Thu Jun 25, 2020 5:50 pm

Garden spade time:
That's way too much work.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4601
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Finding A Character (.) In A String - Return Position

Post by richmond62 » Thu Jun 25, 2020 5:56 pm

jacque wrote:
Thu Jun 25, 2020 5:50 pm
Garden spade time:
That's way too much work.
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. 8)

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5418
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Finding A Character (.) In A String - Return Position

Post by jacque » Thu Jun 25, 2020 6:12 pm

Yup, so it seems, but I got a bit "offset" by domestic problems here.
That's okay, the whole world has domestic problems right now. :)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4601
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Finding A Character (.) In A String - Return Position

Post by richmond62 » Fri Jun 26, 2020 9:20 am

I'm not entirely convinced that this is advantageous in any way:

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
-
Screenshot 2020-06-26 at 11.17.29.png
-

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 3 times

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5418
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Finding A Character (.) In A String - Return Position

Post by jacque » Fri Jun 26, 2020 6:01 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bogs
Posts: 4692
Joined: Sat Feb 25, 2017 10:45 pm

Re: Finding A Character (.) In A String - Return Position

Post by bogs » Fri Jun 26, 2020 8:20 pm

jacque wrote:
Fri Jun 26, 2020 6:01 pm
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.
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) :?
Image

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4601
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Finding A Character (.) In A String - Return Position

Post by richmond62 » Sat Jun 27, 2020 9:07 am

Screenshot 2020-06-27 at 11.05.14.png
-

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 1 time

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4601
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Finding A Character (.) In A String - Return Position

Post by richmond62 » Sat Jun 27, 2020 9:34 am

You can find all occurrences of a character by using offset's optional third parameter.
You can, but it is a "right fudge" doing it:

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
This is the inside of a repeat loop . . .

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.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5418
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Finding A Character (.) In A String - Return Position

Post by jacque » Sat Jun 27, 2020 5:09 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Android Deployment”