String position

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
p4tr1ck
Posts: 36
Joined: Mon Jun 14, 2010 4:58 pm

String position

Post by p4tr1ck »

How would I get a char at a given position in a string?
For example, 0 in string "Hello" would be "H".
Thanks.
bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4219
Joined: Sun Jan 07, 2007 9:12 pm

Re: String position

Post by bn »

Patrick,

Rev starts counting characters (chars) at 1.
To get at a char just say "put char x of myVarible/myField into myOtherVariable/myOtherField

Code: Select all

on mouseUp
   put "Hello" into tData
   repeat with i = 1 to the length of tData
      put "char " & i & ":" &&  char i of tData
      wait 1 second
   end repeat
   put empty
end mouseUp
this puts the chars of Hello one by one into the message box
regards
Bernd
p4tr1ck
Posts: 36
Joined: Mon Jun 14, 2010 4:58 pm

Re: String position

Post by p4tr1ck »

Thanks for the help :)
stevewhyte
Posts: 25
Joined: Mon Oct 18, 2010 6:32 pm

Re: String position

Post by stevewhyte »

Hi,

I'm trying to find the position of a character within a string.

Here is the set up:

put "abcdefghijklmnopqrstuvwxyz" into alpha

If I wanted to find the position of letter "c", how would I carry this out?

The answer produced by the program should be 3.

Regards,

Steven
Klaus
Posts: 14325
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: String position

Post by Klaus »

Hi Steven

...
put "abcdefghijklmnopqrstuvwxyz" into alpha
answer offset("c",alpha)
...

Best

Klaus
stevewhyte
Posts: 25
Joined: Mon Oct 18, 2010 6:32 pm

Re: String position

Post by stevewhyte »

You're a legend! Thank you so much!
Post Reply