AS3如何拆分数组中的字符串并将它们放入新数组中?

时间:2014-04-27 20:46:47

标签: arrays string actionscript-3 split

你有这个列表,你想跟踪它:

“Bim的”, “范”, “汜”, “的Bam”, “贝姆”, “比姆”, “Beeem”

var list: Array = new Array ( "Bim and Bum","Bom, Bam","Bem,Beem and Beeem"); var ordnenList:Array = list.map(
function (item:*,index:int, array:Array):Array{ return (item as String).replace(" und " , ", ").split(", "); } );
 我这样开始,但它不起作用。 谢谢: - )

1 个答案:

答案 0 :(得分:0)

通常最简单的方法是使用for in循环来实现这些目的。我们可以像往常一样实现string.replace函数,然后再拆分字符串。

我将所有这些停放在一个功能中,以使其更容易。

function breakDownArray(list:Array):Array
{
    var newList:Array = [];             //Use this array for storing changes, so we don't mess up the for in loop.
    for each(var s:String in list)          //For each item in array...
    {
        //We're going to use , as the character to split by in a moment...
        s = s.replace(" and ", ",");    //Replace the " and " separator with a comma.
        //You can keep putting additional search terms in here.

        var splitString:Array = s.split(",");   //Split string using ",".
        newList = newList.concat(splitString); //We combine our newList array with the splitString array.
    }

    return newList;         //Return newList.
}

禁止错误,这里(代码未经测试),输出应该如下所示......

breakDownArray["Bim and Bum","Bom, Bam","Bem,Beem and Beeem"];
//"Bim", "Bum", "Bom", "Bam", "Bem", "Beem", Beeem"

将命令组合成单行代码可能很诱人,就像你最初那样,但要注意你正在进行错误跟踪。有一些例外,但作为一般经验法则,一行代码应完成一项任务,最多两项。

相关问题