Need help Spliting a strings.

J

Jack

Hello,

I need to split:


2 1066 1.30 172.90 1065.9 -14.2 3.0 -13.3 0.1
3 1528 1.00 188.10 1527.8 -23.3 3.0 -20.9 0.1
4 2007 0.60 182.60 2006.7 -30.0 2.3 -25.9 0.1
5 2484 1.00 195.20 2483.7 -36.5 1.1 -30.5 0.1
6 2962 0.90 200.90 2961.6 -44.0 -1.3 -35.3 0.0
7 3104 1.50 237.50 3103.6 -46.1 -3.3 -35.8 0.7

Into

2
1066
1.30
172.90
1065.9
-14.2
3.0
-13.3
0.1

And do this for each Line.

I tried split(" "), but it did not work. I would like to parse from
non-space char to non-space char.
Thanks in advance for any help!!
Thanks,
Jack
 
Z

zacks

Hello,

I need to split:

2 1066 1.30 172.90 1065.9 -14.2 3.0 -13.3 0.1
3 1528 1.00 188.10 1527.8 -23.3 3.0 -20.9 0.1
4 2007 0.60 182.60 2006.7 -30.0 2.3 -25.9 0.1
5 2484 1.00 195.20 2483.7 -36.5 1.1 -30.5 0.1
6 2962 0.90 200.90 2961.6 -44.0 -1.3 -35.3 0.0
7 3104 1.50 237.50 3103.6 -46.1 -3.3 -35.8 0.7

Into

2
1066
1.30
172.90
1065.9
-14.2
3.0
-13.3
0.1

And do this for each Line.

I tried split(" "), but it did not work. I would like to parse from
non-space char to non-space char.
Thanks in advance for any help!!
Thanks,
Jack

I assume you actually did Split(variablename, " ")?

Actually, Split(varibablename) is equivalen, as the space character is
the default delimiter for the Space function.
 
C

Chris Dunaway

Hello,

I need to split:

2 1066 1.30 172.90 1065.9 -14.2 3.0 -13.3 0.1
3 1528 1.00 188.10 1527.8 -23.3 3.0 -20.9 0.1
4 2007 0.60 182.60 2006.7 -30.0 2.3 -25.9 0.1
5 2484 1.00 195.20 2483.7 -36.5 1.1 -30.5 0.1
6 2962 0.90 200.90 2961.6 -44.0 -1.3 -35.3 0.0
7 3104 1.50 237.50 3103.6 -46.1 -3.3 -35.8 0.7

Into

2
1066
1.30
172.90
1065.9
-14.2
3.0
-13.3
0.1

And do this for each Line.

I tried split(" "), but it did not work. I would like to parse from
non-space char to non-space char.
Thanks in advance for any help!!
Thanks,
Jack

What do you mean it did not work? Did you get an error? Are those
indeed spaces in between each number? Do you need to use them as
numbers, for calculations for example?

Dim numbers As String() = oneOfYourStrings.Split(" ")

For Each number As String In numbers
Console.WriteLine(number)
Next

That should work. If you need to convert each one to a number, you
can try Decimal.TryParse, or Double.TryParse, etc.

Good Luck
 
J

Jack

Hello,

Thanks for the help, but I need to return just the Item with no Blank
string.

Thanks,

Jack
 
C

Chris Dunaway

Thanks for the help, but I need to return just the Item with no Blank
string.

I'm not sure what you mean by this. The Split method does not return
any spaces. If you happen to have more than one consecutive space in
the original string, you could use the alternate overload and specify
the RemoveEmptyEntries option.

Please explain further what problem you are having with String.Split

Chris
 
D

diAb0Lo

Hello,

I need to split:

2 1066 1.30 172.90 1065.9 -14.2 3.0 -13.3 0.1
3 1528 1.00 188.10 1527.8 -23.3 3.0 -20.9 0.1
4 2007 0.60 182.60 2006.7 -30.0 2.3 -25.9 0.1
5 2484 1.00 195.20 2483.7 -36.5 1.1 -30.5 0.1
6 2962 0.90 200.90 2961.6 -44.0 -1.3 -35.3 0.0
7 3104 1.50 237.50 3103.6 -46.1 -3.3 -35.8 0.7

Into

2
1066
1.30
172.90
1065.9
-14.2
3.0
-13.3
0.1

And do this for each Line.

I tried split(" "), but it did not work. I would like to parse from
non-space char to non-space char.
Thanks in advance for any help!!
Thanks,
Jack

Hi there,

It puts every single string in a array position (tCad) and keep a copy
of the original string:

Dim Cad As String = "2 1066 1.30 172.90 1065.9 -14.2 3.0 -13.3 0.1
"
Dim tmpCad As String
Dim tCad(20) As String
Dim i, a As Integer

tmpCad = Cad
i = 0
While Len(tmpCad) > 0
a = InStr(tmpCad, " ", CompareMethod.Binary)
tCad(i) = Mid(tmpCad, 1, a - 1)
tmpCad = Mid(tmpCad, a + 1, Len(tmpCad))
i += 1
End While

Cheers
 
J

Jack

Hello,

How do I do that?

Thanks,

Jack

Chris Dunaway said:
I'm not sure what you mean by this. The Split method does not return
any spaces. If you happen to have more than one consecutive space in
the original string, you could use the alternate overload and specify
the RemoveEmptyEntries option.

Please explain further what problem you are having with String.Split

Chris
 
J

Jack

Sorry Need to split:

MWD 11409.00 3.00 298.30 N61.7 W 31 11404.89 144.51 N
76.99 W 123.27 145.33 301.99 5.08 1.81 -101.8 -5.8
15.4 112.5 3948.2 -928.9 105.9 1.8
4.0
MWD 11441.00 6.10 293.80 N66.2 W 32 11436.78 147.05 N
78.07 W 125.56 147.85 301.87 9.74 9.69 -14.06 -5.3
15.4 112.5 3950.7 -960.8 106.4 9.7
11.3
MWD 11473.00 8.60 294.00 N66.0 W 32 11468.52 151.14 N
79.73 W 129.30 151.91 301.66 7.81 7.81 0.62 -4.9
15.2 112.5 3954.8 -992.5 106.9 7.8
12.8
MWD 11505.00 10.60 294.90 N65.1 W 32 11500.07 156.48 N
81.94 W 134.16 157.20 301.42 6.27 6.25 2.81 -4.6
15.1 112.5 3960.1 -1024.1 107.5 6.2
14.0
MWD 11537.00 11.10 295.20 N64.8 W 32 11531.49 162.50 N
84.49 W 139.61 163.19 301.18 1.57 1.56 0.94 -4.4
15.0 112.5 3966.2 -1055.5 108.0 1.6
11.9
MWD 11570.00 12.40 296.40 N63.6 W 33 11563.80 169.22 N
87.42 W 145.66 169.88 300.97 4.01 3.94 3.64 -4.1
15.0 112.5 3972.9 -1087.8 108.6 3.9
14.5
P-Bit 11624.00 14.03 296.40 N63.6 W 54 11616.37 181.56 N
92.91 W 156.72 182.19 300.66 3.02 3.02 0.00 -3.8
15.1 112.5 3985.2 -1140.4 109.5 3.0
15.7

into Columns.

Thanks for you help,

Jack
 
S

sergejusz

Sorry Need to split:

MWD 11409.00 3.00 298.30 N61.7 W 31 11404.89 144.51 N
76.99 W 123.27 145.33 301.99 5.08 1.81 -101.8 -5.8
15.4 112.5 3948.2 -928.9 105.9 1.8
4.0
MWD 11441.00 6.10 293.80 N66.2 W 32 11436.78 147.05 N
78.07 W 125.56 147.85 301.87 9.74 9.69 -14.06 -5.3
15.4 112.5 3950.7 -960.8 106.4 9.7
11.3
MWD 11473.00 8.60 294.00 N66.0 W 32 11468.52 151.14 N
79.73 W 129.30 151.91 301.66 7.81 7.81 0.62 -4.9
15.2 112.5 3954.8 -992.5 106.9 7.8
12.8
MWD 11505.00 10.60 294.90 N65.1 W 32 11500.07 156.48 N
81.94 W 134.16 157.20 301.42 6.27 6.25 2.81 -4.6
15.1 112.5 3960.1 -1024.1 107.5 6.2
14.0
MWD 11537.00 11.10 295.20 N64.8 W 32 11531.49 162.50 N
84.49 W 139.61 163.19 301.18 1.57 1.56 0.94 -4.4
15.0 112.5 3966.2 -1055.5 108.0 1.6
11.9
MWD 11570.00 12.40 296.40 N63.6 W 33 11563.80 169.22 N
87.42 W 145.66 169.88 300.97 4.01 3.94 3.64 -4.1
15.0 112.5 3972.9 -1087.8 108.6 3.9
14.5
P-Bit 11624.00 14.03 296.40 N63.6 W 54 11616.37 181.56 N
92.91 W 156.72 182.19 300.66 3.02 3.02 0.00 -3.8
15.1 112.5 3985.2 -1140.4 109.5 3.0
15.7

into Columns.

Thanks for you help,

Jack







- Show quoted text -

Hi,
here is simple program that do this stuff. No check whether number of
columns is permanent from line to line. Elements in line are separated
with space(s) or tabs.
' CODE STARTS HERE
Imports System.Collections.Generic
Imports System.IO
Imports System.Text

Module Module1


Const Space As Char = Chr(32)
Const Tab As Char = Chr(9)

Sub Main()

Dim Elements As New List(Of String)
Dim LineNum As Integer = 0
Dim ColNum As Integer = 0


Dim f As New StreamReader("c:\work\dotnet\cols1.txt")


Dim ColNumTest As Integer = 0

While Not f.EndOfStream
Dim str() As Char = (f.ReadLine()).ToCharArray

ColNum = 0
Dim elem As String = String.Empty
For Each c As Char In str
If c = Space Or c = Tab Then
If elem <> String.Empty Then
Elements.Add(elem)
ColNum += 1
elem = String.Empty
End If
Else
elem &= c
End If
Next

' line completed here

LineNum += 1
End While

f.Close()


If LineNum > 1 AndAlso ColNum > 1 Then

Dim column As New List(Of String)
For col As Integer = 0 To ColNum - 1
Console.Write(String.Format("Column No.{0} ", col +
1))
For line As Integer = 0 To LineNum - 1
column.Add(Elements(col + line * ColNum))
Console.Write(Elements(col + line * ColNum) & "
")
Next
Console.WriteLine()
' at this point current column is completed
'and you can do with your column what you want
Next
End If

Console.ReadLine()

End Sub

End Module
' CODE END HERE

RESULT:
Column No.1 MWD MWD MWD MWD MWD MWD
Column No.2 11409.00 11441.00 11473.00 11505.00 11537.00
11570.00
Column No.3 3.00 6.10 8.60 10.60 11.10 12.40
Column No.4 298.30 293.80 294.00 294.90 295.20 296.40
Column No.5 N61.7 N66.2 N66.0 N65.1 N64.8 N63.6
Column No.6 W W W W W W
Column No.7 31 32 32 32 32 33
Column No.8 11404.89 11436.78 11468.52 11500.07 11531.49
11563.80
Column No.9 144.51 147.05 151.14 156.48 162.50 169.22
Column No.10 N N N N N N
Column No.11 76.99 78.07 79.73 81.94 84.49 87.42
Column No.12 W W W W W W
Column No.13 123.27 125.56 129.30 134.16 139.61 145.66
Column No.14 145.33 147.85 151.91 157.20 163.19 169.88
Column No.15 301.99 301.87 301.66 301.42 301.18 300.97
Column No.16 5.08 9.74 7.81 6.27 1.57 4.01
Column No.17 1.81 9.69 7.81 6.25 1.56 3.94
Column No.18 -101.8 -14.06 0.62 2.81 0.94 3.64
Column No.19 -5.8 -5.3 -4.9 -4.6 -4.4 -4.1
Column No.20 15.4 15.4 15.2 15.1 15.0 15.0
Column No.21 112.5 112.5 112.5 112.5 112.5 112.5
Column No.22 3948.2 3950.7 3954.8 3960.1 3966.2 3972.9
Column No.23 -928.9 -960.8 -992.5 -1024.1 -1055.5 -1087.8
Column No.24 105.9 106.4 106.9 107.5 108.0 108.6
Column No.25 1.8 9.7 7.8 6.2 1.6 3.9
Column No.26 4.0 11.3 12.8 14.0 11.9 14.5

HTH
Kind regards
Serge
http://www.sergejusz.com
 
Top