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



我正在使用一个将csv数据转换为多维数组的函数。然后,我将遍历字符,以查找数组中字符串处理方式的特定情况。举个例子,如果我有一个字符串——"this is a string, yeah"——那么我要确保不计算字符串中的逗号,因为它位于字符串的引号之间。无论如何,在下面的函数中,我在结果中丢失了一些空间。我得到的不是"thisisAS3",而是"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;
    }

谢谢你的帮助,非常感谢!

这似乎可以使用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;

如果没有引号,并且字符不是空白,则添加

那么,当你不在引号中并且遇到空格时,它不被附加到字符串中吗?

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

扩展Taytay的一行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, yeahn");
        }
        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);
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新