2015-05-29 46 views
2
FASTER,WW0011,"CTR ,REURN,ALT TUBING HELIUM LEAK",DEFAULT test,1,3.81,test 

我需要得到下面一行的結果作爲拆分在VB.net

Arr(0) =faster 
Arr(1) =WW0011 
Arr(2) =CTR ,REURN,ALT TUBING HELIUM LEAK 
Arr(3) =DEFAULT test 
Arr(4) =faster 
Arr(5) = 1 
Arr(6)=3.81 
Arr(7) = test 

我試着用拆分,但問題是在編曲(2) 任何人都可以,請給我一個解決方案

+0

你可以在這裏得到一個答案襯墊(交換撇號雙引號)http://stackoverflow.com/a/3147901/590510 – alergy

+0

哪裏來自你得到的第二個_faster_項目('Arr(4)')? – Sehnsucht

回答

3

我用這個功能很多自己

Private Function splitQuoted(ByVal line As String, ByVal delimeter As Char) As String() 
Dim list As New List(Of String) 

    Do While line.IndexOf(delimeter) <> -1 
     If line.StartsWith("""") Then 
      line = line.Substring(1) 
      Dim idx As Integer = line.IndexOf("""") 
      While line.IndexOf("""", idx) = line.IndexOf("""""", idx) 
       idx = line.IndexOf("""""", idx) + 2 
      End While 
      idx = line.IndexOf("""", idx) 
      list.Add(line.Substring(0, idx)) 
      line = line.Substring(idx + 2) 
     Else 
      list.Add(line.Substring(0, Math.Max(line.IndexOf(delimeter), 0))) 
      line = line.Substring(line.IndexOf(delimeter) + 1) 
     End If 
    Loop 
    list.Add(line) 
    Return list.ToArray 
End Function 
+0

真棒,謝謝!!!!!! – SSJGSS

+1

或者你可以使用TextFieldParser類來處理這種情況。 –

-1

使用一個for循環來增槎遍歷串字符[R!

4

你可以使用TextFieldParser類來處理這種情況。將HasFieldEnclosedInQuotes屬性設置爲true。下面是從MSDN(輕微改變)一個例子:

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("c:\logs\bigfile") 

    MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited 
    MyReader.Delimiters = New String() {","} 

    'Set this to ignore commas in quoted fields. 
    MyReader.HasFieldsEnclosedInQuotes = True 

    Dim currentRow As String() 
    'Loop through all of the fields in the file. 
    'If any lines are corrupt, report an error and continue parsing. 
    While Not MyReader.EndOfData 
     Try 
      currentRow = MyReader.ReadFields() 
      ' Include code here to handle the row. 
     Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException 
      MsgBox("Line " & ex.Message & " is invalid. Skipping") 
     End Try 
    End While 
End Using