splitting a string

前端 未结 6 1912
不思量自难忘°
不思量自难忘° 2021-01-26 16:52

i have the following string:

http://pastebin.com/d29ae565b

i need to separate each value and put it in an array. typically it would be done by \".split\". howeve

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-26 17:35

    It sounds to me like you need to split things twice. Read the file line by line into an array, or better yet a List(Of String), and then iterate through each "line" in the List and do a subsequent split based on the space.

    As you go through each line, you can add the first element into your result array, list.

    EDIT: Since you're having some troubles, try out this code and see if it works for you:

    Dim lstResulst As New List(Of String)
    Dim lstContent As New List(Of String)
    Dim LineItems() As String
    Dim objStreamReader as StreamReader
    
    objStreamReader = File.OpenText(FILENAME)
    
    While objStreamReader.Peek() <> -1
      lstContent.Add(objStreamReader.ReadLine())
    End While
    
    objStreamReader.Close()
    

    This reads all of your files line per line into a List(Of String). Then from there you can do this:

    For Each CurrLine As String In lstContent
       LineItems = CurrLine.Split(Char.Parse(" "))
       lstResults.Add(LineItems(0))
    Next
    

    That'll split each item into an array and you can dump the first item of the split into a new List(Of String). You can easily dump this into a list of Decimals or whatever and simply wrap the CurrLine.Split around the appropriate conversion method. The rest of the items on the line will be available in the LineItems array as well.

提交回复
热议问题