Irfan
Irfan

Reputation: 303

Testing anOrdered Collection

|email addressList|
email:= '<[email protected]>,abcd ,<[email protected]>'.
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."]

Upvotes: 1

Views: 134

Answers (2)

Uko
Uko

Reputation: 13396

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.

Upvotes: 3

Norbert Hartl
Norbert Hartl

Reputation: 10851

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 ]

Upvotes: 3

Related Questions