私は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;
}
ご協力いただきありがとうございます。