Isolating substring to certain character

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
stoavio
Posts: 43
Joined: Sun Jun 30, 2013 2:09 am

Isolating substring to certain character

Post by stoavio » Sun Oct 26, 2014 5:40 pm

Hi all -

What is the best way to select the text (for example: an email address) up to a certain character? For example, if I only want the username of this email address "bobsacamono" which obviously goes to "@" what is the most efficient way or isolating my substring?

bobsacamono@seinfeld.com

This was my method, but it seems like there are too many steps involved for accomplishing something that should be a 1 or 2 liner...

Code: Select all

on mouseUp
   put "bobsacamono@seinfeld.com" into tEmail
   put offset ("@", tEmail) into tBreakpoint
   
   put char 0 to tBreakpoint - 1 of tEmail into tEmail
   answer tEmail
end mouseUp
Is there a more efficient method? Thanks!

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Re: Isolating substring to certain character

Post by sefrojones » Sun Oct 26, 2014 6:00 pm

This works:

Code: Select all

on mouseUp
   put "bobsacamono@seinfeld.com" into tEmail
   set the itemdelimiter to "@"
   answer item 1 of tEmail
end mouseUp
--sefro

stoavio
Posts: 43
Joined: Sun Jun 30, 2013 2:09 am

Re: Isolating substring to certain character

Post by stoavio » Sun Oct 26, 2014 10:00 pm

Hi Sefro -

Good suggestion, however, I started off using that but in my actual use-case I am parsing through a directory containing multiple emails using a loop so then I get junk items created as a result of breaking apart the string like that. In this case, item 2 is junk.

item 1 = bobsacamono
item 2 = @seinfeld.com

I guess I just need to figure out how to isolate only item 1 in my loop. I'll take another look and come back with my code.
Thanks!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Isolating substring to certain character

Post by bn » Sun Oct 26, 2014 10:37 pm

Hi Stoavio,

your could try

Code: Select all

word - 1 of item 1
like this in a button, two fields, field 1 has the email addresses, one on each line.

I tested with this, maybe I forgot some formats that would break this.

Code: Select all

on mouseUp
   put field 1 into tData
   set the itemDelimiter to "@"
   
   repeat for each line anAddress in tData
      put word - 1 of item 1 of anAddress & cr after tCollect
   end repeat
   delete last char of tCollect -- a return
   put tCollect into field 2
end mouseUp

William Jamieson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 212
Joined: Fri Feb 01, 2013 1:31 am
Contact:

Re: Isolating substring to certain character

Post by William Jamieson » Tue Oct 28, 2014 8:38 am

I think SefroJones was pointing out the first part of the solution.
Here is an example if you have an array of emails

Code: Select all

local emailArray

set the itemdel to "@"
put the keys of emailArray into tKeys
repeat with x = 1 to the number of lines in tKeys
put item 1 of emailArray[(line x of tKeys)] into emailArray[(line x of tKeys)]
end repeat
This will cause your array to go from
[joe@gmail.com]
[alice@hotmail.com]
[john@yahoo.com]
[steve@msn.com]

To
[joe]
[alice]
[john]
[steve]

And there will be not ".com" 's floating around in cyber space causing bloat because the data is overwritten. To not overwrite, then change the second array's name to something slightly different (EX: emailArray2)

stoavio
Posts: 43
Joined: Sun Jun 30, 2013 2:09 am

Re: Isolating substring to certain character

Post by stoavio » Tue Oct 28, 2014 10:58 am

Hi William Jamieson -

I like that solution as well, very elegant! I'm beginning to see that there is "more than one way to skin a cat" in LiveCode. I am always looking for the fastest and simplest solutions, which I think you all have given me.

Thanks again everyone.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Isolating substring to certain character

Post by [-hh] » Wed Oct 29, 2014 11:33 am

Yet another way to find all usernames (as first part of of 'email-words') in any string:
for example in
"He, and also she, use office: joe@iMail.com / private: alice@youMail.com"

Code: Select all

function userNames S
  set linedelimiter to "@"
  repeat for each line L in S
    put cr & last word of L after T
  end repeat
  delete char 1 of T; return T
end userNames
shiftLock happens

Post Reply