Page 1 of 1
Isolating substring to certain character
Posted: Sun Oct 26, 2014 5:40 pm
by stoavio
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!
Re: Isolating substring to certain character
Posted: Sun Oct 26, 2014 6:00 pm
by sefrojones
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
Re: Isolating substring to certain character
Posted: Sun Oct 26, 2014 10:00 pm
by stoavio
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!
Re: Isolating substring to certain character
Posted: Sun Oct 26, 2014 10:37 pm
by bn
Hi Stoavio,
your could try
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
Re: Isolating substring to certain character
Posted: Tue Oct 28, 2014 8:38 am
by William Jamieson
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)
Re: Isolating substring to certain character
Posted: Tue Oct 28, 2014 10:58 am
by stoavio
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.
Re: Isolating substring to certain character
Posted: Wed Oct 29, 2014 11:33 am
by [-hh]
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