2011-10-25 10 views
0

私はcsvデータを多次元配列に変換する関数を扱っています。文字列を処理して、配列内の文字列を処理する方法を特定の状況で見つけようとしています。 1つの例は、 "これは文字列です、ええ"のような文字列を持っている場合です。文字列の引用符の間にあるので、文字列のコンマを数えないようにします。とにかく次の関数では、結果にスペースがなくなってしまったことがあります。 「これはAS3です」というより、「thisisAS3」を得ています。空白は、引用符を含む文字列でのみ使用可能なようです。誰でも、このコード部分の問題がどこにあるのかを知っていますか?CSV配列関数の文字列のスペースを紛失

 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; 

    } 

ご協力いただきありがとうございます。

答えて

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; 

です。

したがって、引用符で囲まれておらず、空白に遭遇したときは、文字列に追加されません。

実際、これはRegExの1行で実現できます。 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); 
     } 

    } 
} 
関連する問題