Reputation: 60902
i have the following string:
i need to separate each value and put it in an array. typically it would be done by ".split". however i need it to be split in this order: 0, 50, 100, 200, 400 etc..
does anyone know how to do this? please note that i need to do this in VB
in other words, i need it to read the rows left to right. i have no problem separating each number, i just need it to read it in the specified order.
thank you all for your suggestions. ive tried the regexp and i forgot to mention that after each line there is a line break. i am not sure if this would impact the regex, but in any case, after i do the regex, i get the following order: 0, 6.65, 84..??, 35.... etc
i am not getting the order i need as stated above
expected results: 0, 50, 100, 100, 200, 400, 218, 9.8, ???, 6.65, 6.31 etc...
i am going to follow some of the suggestions below by splitting up the string into separate lines initially. this code almost does that:
Dim fields() As String
fields = calculationText.Split(vbCrLf)
unfortunately for some reason the spaces are lost. when i look into the array, all the spaces between numbers are lost. why?????????
Upvotes: 0
Views: 872
Reputation: 29755
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.
Upvotes: 5
Reputation: 6356
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.)
Upvotes: 2
Reputation: 1749
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(",")
Upvotes: 1
Reputation: 11587
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
Upvotes: 2
Reputation: 17749
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.
Upvotes: 2
Reputation: 22787
Split it up by line, then use this RegEx to match:
(\d+\.\d+)|(\?\?\?\?\?\?)
Upvotes: 3