1

I have a string like this:

var tempSting:String = "[email protected];[email protected]"

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{[email protected], [email protected]}

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

2 Answers 2

10
var tempString:String="[email protected];[email protected]";
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("[email protected]") will give you the index at which [email protected] exists in the arraycollection (or -1 if not contained)

tempAc.contains("[email protected]") 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="[email protected]";
if(!tempAc.contains(newEmailId)) {
    tempAc.addItem(newEmailId);
}
Sign up to request clarification or add additional context in comments.

8 Comments

Is there a reason to do this rather than just passing the tempArray in the ArrayCollection constructor? Honest question, I don't work much with ArrayCollections.
@shanethehat: Your code will create a new arraycollection. This will then contain only the 2 emails given in tempString. If you have a pre- populated arraycollection, then looping is the way to append the array
I see, although I think you meant to use a semi-colon rather than a comma in your last code block. +1
@shanethehat, you're right. I've been working with csv files all day long, that one kinda crept in :) Corrected it now :D
@Prana: i have one more help.. I had added 2 mail id's to ArrayCollection. if i add one more mail id, i have to check the duplicate list from the arraycollection. can i know how to do this?
|
6
var tempString:String = "[email protected];[email protected]";
tempAC:ArrayCollection = new ArrayCollection(tempString.split(";"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.