1
|email addressList|
email:= '<abc@gmail.com>,abcd ,<abcef@domail.com>'.
addressList:= MailAddressParser addressesIn:email.

Above code will get me an Ordered collection which will include all the strings, though 'abcd' is not a valid email id its still there in collection.

My question is how can i remove Invalid email address which don't include '@'.

addressList do[:ele | " How can i check each item if it includes @ , if not remove it."]
Irfan
  • 303
  • 1
  • 8

2 Answers2

3

You can use

addressList select: [ :email | email includes: $@ ]

if you're looking just for one character like @ (as was suggested by @dh82), or if you'll need to look for a substring you can use:

addressList select: [ :email | email includesSubString: '@' ]

select: creates a new collection that with the elements that match the condition specified in a block.

Uko
  • 13,134
  • 6
  • 58
  • 106
3

You can take several approaches:

Do exactly what you've asked for (deleting from the existing collection)

addressList removeAllSuchThat: [:string| (string includes: $@) not ]

Or you can generate a new list that includes only the valid elements like Uko proposed. Here you exchange addressList with the new list.

addressList select: [:string| string includes: $@ ]

Or you can generate a new list that excludes the invalid elements

addressList reject: [:string| (string includes: $@) not ]
Norbert Hartl
  • 10,481
  • 5
  • 36
  • 46