使用CSV到数组函数的字符串中丢失的空格

时间:2011-10-25 02:30:55

标签: flash actionscript-3 csv multidimensional-array spaces

我正在使用一个将csv数据转换为多维数组的函数。然后,我将通过字符查找在数组中处理字符串的方式的特定情况。一个例子是如果我有一个字符串 - “这是一个字符串,是的” - 然后我确保不计算字符串中的逗号,因为它在该字符串的引号之间。无论如何在以下函数中我有一些在结果中丢失空格的方法。而不是得到“这是AS3”我得到“thisisAS3”。空格似乎只在带引号的字符串中可用。任何人都知道这段代码中的问题在哪里?

        function CSVtoArray(csv:String):Array {
        var inQuotes:Boolean = false;
        var field:String = "";
        var finalData:Array = new Array();
        finalData.push(new Array());
        var line:int = 0;
        //iterate each character
        for(var i:int = 0; i < csv.length; i++) {
            var c:String = csv.charAt(i);
            var n:String = csv.charAt(i+1);
            var ad:Boolean = false;  
            //if the quote repeats, add the character
            if(inQuotes && c == "\"" && n == "\"") {
                field += c; 
            }            
            //if we are inside quotes, add the character
            if(inQuotes && c != "\"") {
                field += c;    
            }     
            //if we are not inside quotes...
            if(!inQuotes && c != "\"") {
                //if this character is a comma, start a new field
                if(c == ",") {
                    finalData[line].push(field);
                    field = "";   
                //if this character is a newline, start a new line
                } else if(c == "\n") {
                    finalData[line].push(field);
                    finalData.push(new Array());
                    line++;
                    field = "";     
                //if this is not leading or trailing white space, add the character
                } else if(c != " " && c != "\t" && c != "\r") {
                    field += c;
                }            
            }      
            //if this is a quote, switch inQuotes
            if(c == "\"") {
                inQuotes = !inQuotes;
            }      
        }      
        //add last line
        finalData[line].push(field);
        //if the last line does not have the same length as the first, remove it
        if(finalData[line].length < finalData[0].length) finalData.pop();



        //return the resulting array
        return finalData;

    }

感谢您对此提供任何帮助,我们非常感谢!

1 个答案:

答案 0 :(得分:0)

这似乎可以使用Tokenizer类,或者已经存在的一些解析器。

当我执行你的功能时:

var result:String = CSVtoArray("\"this is a string, yeah\"");

它按预期工作 - 我得到一个带空格的字符串。

您的逻辑仅适用于带引号的字符串:

//if we are not inside quotes...
if(!inQuotes && c != "\"") {
// ...
    //if this is not leading or trailing white space, add the character
    } else if(c != " " && c != "\t" && c != "\r") {
        field += c;

如果您没有引号,并且字符不是空格,则会添加。

所以,当你没有引号并遇到空格时,它不会附加到字符串中。

实际上,这可以通过1行RegEx来完成。

扩展Taytay's单行CSV解析器,这是一个示例实现:

<强> CsvParser.as

package
{
    import flash.display.Sprite;

    public class CsvParser extends Sprite
    {
        public function CsvParser()
        {
            var set1:Array = CSVtoArray("\"this is a string, yeah\"\n");
            var set2:Array = CSVtoArray("this is a string, yeah\n");
        }

        public function CSVtoArray(csv:String):Array
        {
            // split csv in to rows
            var rows:Array = csv.split("\n");

            // for every row...
            for (var x:uint = 0; x < rows.length; x++)
            {
                var columns:Array = csv.split(/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/g);

                for (var y:uint = 0; y < columns.length; y++)
                {
                    // trim leading / trailing whitespace
                    columns[y] = columns[y].replace(/^\s+|\s+$/g, '');
                }

                rows[x] = columns;
            }

            return (rows);
        }

    }
}