RKCY
RKCY

Reputation: 4135

How to split a String and add to an ArrayCollection

I have a string like this:

var tempSting:String = "abc@abc.com;xyz@xyz.com"

I want to add this String into the ArrayCollection. And the above String should be divided by mail id and remove the ; symbol and need to add asArrayCollection

tempAc:Arraycollection = new ArrayCollection{abc@abc.com, xyz@xyz.com}

Please help me to add the split String into the ArrayCollection.

Upvotes: 1

Views: 18026

Answers (2)

pho
pho

Reputation: 25490

var tempString:String="abc@abc.com;xyz@xyz.com";
var tempArray:Array=tempString.split(";");

//tempAc is a predefined and presumably prepopulated arraycollection    

for each(var email:String in tempArray) {
    tempAc.addItem(email);
}

EDIT Now that I see Shane's answer I must add the following:

This code will append the array to the arraycollection. If you just want to create a new arraycollection, all you need to do is:

var tempAc:ArrayCollection=new ArrayCollection(tempArray);

or in 1 line,

var tempAc:ArrayCollection=new ArrayCollection(tempString.split(";"));

UPDATE - to answer questions in the comments:

tempAc.getItemAt(i) will give you the email id at the i th position

tempAc.getItemIndex("someone@email.com") will give you the index at which someone@email.com exists in the arraycollection (or -1 if not contained)

tempAc.contains("someone@email.com") will return true or false depending on if the string is contained or not in the arraycollection

So, to check for duplicate ids, all you got to do is:

var newEmailId:String="someone@email.com";
if(!tempAc.contains(newEmailId)) {
    tempAc.addItem(newEmailId);
}

Upvotes: 10

shanethehat
shanethehat

Reputation: 15570

var tempString:String = "abc@abc.com;xyz@xyz.com";
tempAC:ArrayCollection = new ArrayCollection(tempString.split(";"));

Upvotes: 6

Related Questions