tags:

views:

323

answers:

6
+1  Q: 

splitting a string

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". 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?????????

+5  A: 

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.

Dillie-O
i am trying to do this but for some reason when i split it up into lines, it gets rid of all spaces
I__
I added some code to see if this works for your space elimination issue.
Dillie-O
unfortunately this only works for the first column
I__
Then you need to repeat it; that's just another outer loop.
Svante
I apologize. When I asked the question originally, you had no mention for the rest of the code. I've updated the sample code to split each line into an Array, so you can process the other items as needed through the various indexes within the LineItems array.
Dillie-O
+2  A: 

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.)

Adam V
tried to split but it removed spaces
I__
Do you have to save the spaces? If you do, then you probably need to split by the newline, then read character by character.
Adam V
+3  A: 

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

(\d+\.\d+)|(\?\?\?\?\?\?)
slf
I like this idea if he's getting the data from somewhere other than a file. In this case you would want to use the Regex.Split(input, pattern) method.
Rob Allen
You could also use a RegEx like ((?:\d+\.\d+)|(?:\?{6})) and get the matching results too.
Paulo Santos
cool can you help me implement this in my code? i dont know how to take a string and put it in this format?
I__
+2  A: 

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.

Rob Allen
+2  A: 

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
Paulo Santos
+1  A: 

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(",")
spacemonkeys