splitting a string

前端 未结 6 1911
不思量自难忘°
不思量自难忘° 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:31

    You could use a TextReader read each line separatedly and split the string as needed.

    Function GetNumbers(reader As TextReader) As String()
    
        Dim lst As New List(Of String)
    
        Do While Not reader.EndOfStream
            lst.AddRange(reader.ReadLine().Split(vbTab))
        Loop
    
        Return lst.ToArray()
    
    End Function
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-26 17:39

    Looking at that (without copy/pasting to see how it's actually written), I'd think you could first Split() by the newline character, then Split() each string in that array using the tab character.

    EDIT: Oh, you essentially want to pivot the table and then return the results in order. I'm writing the test code now and will post it once I'm done. (It'll be C#, though.)

    0 讨论(0)
  • 2021-01-26 17:43

    If you are reading that data from a file, you can back up a step and use the ReadLine() method from the StreamReader class.

    The code would look something like this:

    Dim myReader as new StreamReader(strFilePath)
    Dim myLines as new List(Of String)
    
    While Not myReader.EndOfStream
        myLines.Add(myReader.ReadLine())
    end While
    

    Your List(of String) would then contain one String for each row of data.

    0 讨论(0)
  • 2021-01-26 17:43

    Bit long winded and round the house, but a different slant,

    Could you not use the .Split to split the lines, then use RegEx to replace the spaces + tabs with , using RegEx.Replace using the "/s+" pattern

    Or if you have the numbers in one long string, the following code (you might need to tweak the regex a little to include the line feeds) would give you an array of the values (I think)

      Dim matchPattern As String = "\s+"
        Dim patternMatch As New Regex(matchPattern)
        Dim resultString As String = Regex.Replace("0.001    0.0002   3", matchPattern, ",")
        Dim resultStrings() As String = resultString.Split(",")
    
    0 讨论(0)
  • 2021-01-26 17:50

    Split it up by line, then use this RegEx to match:

    (\d+\.\d+)|(\?\?\?\?\?\?)
    
    0 讨论(0)
提交回复
热议问题