2011-05-03 74 views
1

從mysql生成包含一組數據的csv格式,現在我可以導入到datagrid中顯示每個值,但是我有一個問題,它將顯示帶雙引號(「1」,「2」)的值(1,2,3等),刪除雙引號(「」)的最佳方法是什麼?csv in datagrid

"1";"[email protected]";"asdasdasd";"1302531993" 
"2";"[email protected]";"werwetewt";"234235" 
"3";"[email protected]";"werwetewt";"234235" 

回答

1

嘗試使用str.replace("\"", "");其中str是修剪的字符串。 AS3中的String.replace查找第一個arg的所有出現,並用第二個arg替換它們。如果你願意的話,這兩種都可以是正則表達式,但在你的情況下,一個簡單的轉義字符\"就可以正常工作。

3

您可以嘗試類似如下:

<?xml version="1.0" encoding="utf-8"?> 
<s:Application creationComplete="init(event)" xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark"> 
    <fx:Script> 
    <![CDATA[ 
     import mx.collections.ArrayCollection; 
     import mx.events.FlexEvent; 

     protected function init(event:FlexEvent):void 
     { 
      var beginningQuotesExp:RegExp = /^\s*"/gm; 
      var result:String = csvData.replace(beginningQuotesExp, ""); 
      var endingQuotesExp:RegExp = /"\s*$/gm; 
      result = result.replace(endingQuotesExp, ""); 
      var bodyQuotesExp:RegExp = /"\s*;\s*"/gm; 
      result = result.replace(bodyQuotesExp, ";"); 
      var splitRegExp:RegExp = /\r*\n+|\n*\r+/gm; 
      var lines:Array = result.split(splitRegExp); 
      var dataProvider:ArrayCollection = new ArrayCollection(); 
      for each (var line:String in lines) 
      { 
       var lineArray:Array = line.split(";"); 
       var dataObject:Object = 
        { counter: lineArray[0], email: lineArray[1], name: lineArray[2], phone: lineArray[3] }; 
       dataProvider.addItem(dataObject); 
      } 
      dg.dataProvider = dataProvider; 
     } 
    ]]> 
    </fx:Script> 

    <fx:Declarations> 
     <fx:String id="csvData"> 
      <![CDATA["1";"[email protected]";"asdasdasd";"1302531993" 
"2";"[email protected]";"werwetewt" ; "234235" 
"3";"[email protected]";"werwetewt";"234235"]]> 
     </fx:String> 
    </fx:Declarations> 
    <mx:DataGrid horizontalCenter="0" id="dg" verticalCenter="0"> 
     <mx:columns> 
      <mx:DataGridColumn dataField="counter" headerText="counter" /> 
      <mx:DataGridColumn dataField="email" headerText="email" /> 
      <mx:DataGridColumn dataField="name" headerText="name" /> 
      <mx:DataGridColumn dataField="phone" headerText="phone" /> 
     </mx:columns> 
    </mx:DataGrid> 
</s:Application> 
+0

謝謝你的提示,它的工作漂亮! – Proyb2 2011-05-03 17:33:00