2016-11-22 93 views
1

我已經構建了一個項目,從報表中讀取數據,它用於正常工作,但現在出於某種原因,報告把每件事情都放到字符串中。所以我想修改我的流閱讀器來刪除或忽略引號,因爲它讀取行。VB.net如何從一個streamReader.readline.split()

這是讀取行的部分snipet。

Dim RawEntList As New List(Of Array)() 

Dim newline() As String 
Dim CurrentAccountName As String 
Dim CurrentAccount As Account 
Dim AccountNameExsists As Boolean 
Dim NewAccount As Account 
Dim NewEntry As Entrey 
Dim WrongFileErrorTrigger As String 

ListOfLoadedAccountNames.Clear() 
'opens the file 
Try 
    Dim sr As New IO.StreamReader(File1Loc) 
    Console.WriteLine("Loading full report please waite") 
    MsgBox("Loading full report may take a while.") 
    'While we have not finished reading the file 
    While (Not sr.EndOfStream) 
     'spliting eatch line up into an array 
     newline = sr.ReadLine.Split(","c) 
     'storring eatch array into a list 
     RawEntList.Add(newline) 
    End While 

然後當然我遍歷列表拉出來的信息來填充對象這樣的:

For Each Entr In RawEntList 
    CurrentAccountName = Entr(36) 
    AccountNameExsists = False 
    For Each AccountName In ListOfLoadedAccountNames 
     If CurrentAccountName = AccountName Then 
      AccountNameExsists = True 
     End If 
    Next 

回答

1

你可以只是做

StringName.Replace(ControlChars.Quote, "") 

StringName.Replace(Chr(34), "") 

streamReader.readline.split().Replace(Chr(34), "") 
+0

它不是一個字符串,它是一個字符串數組,它是split()如何工作的。你不能在數組上調用replace。 我想我可以在數組中調用它,就像這個'ent(0).Replace(Char(34),「」)''那樣,這個數值就必須通過50次。女巫已經在運行150,000次或更多次的另一個血液中跑步。它是可移動的,但它是非常無效的。所以,這種排除你的前兩個解決方案,但第三個我可能會嘗試看看它是否有效。 – skyzzle

+1

@SkylineGodzilla:在調用'.Split()'之前,先放上'.Replace()'。而後者可能會返回一個數組,而'ReadLine()'不會:'sr.ReadLine()。Replace(Chr(34),「」).Split(「,」c)'。 –

+0

@WallyModz:OR'StringName.Replace(「」「」c,「」)''。 ;) –

0

如何在拆分之前,在readline之後執行替換?這應該可以節省迭代乘法,或者更好(如果可能),使用File對象的ReadAllText方法替換整個文件(如果數據的格式化方式可以使用&,那麼您有足夠的內存),do你的替換,然後讀取內存中的行來構建你的數組(超快!)。

File.ReadAllText(path)