Reputation: 107
I'm trying to split up the following string into substrings
Dim value as String = "+1.0 A0 -256 B0 -2823" 'this is a line from a txtfile
I want to split the string in the following 3 substrings.
Dim value1 as String= "+1.0"
Dim A0 as String = "-256"
Dim B0 as String = "-2823"
the String is always in the following format:
+## A0 #### B0 ###
I know I can achieve it with the use of value.Replace
and value.Substring
, but I don't know how exactly.
I have tried stuff like this, but it didn't work
value1 = Value.Substring(0, "A0".IndexOf("A0"))
Upvotes: 0
Views: 85
Reputation: 38915
NET contains a powerful String Split method. The source is delimited by spaces, so split on that and grab elements 0, 2 and 4 from the result:
If/when the lines read have a single space between the items as described in one part:
Dim value As String = "+1.0 A0 -256 B0 -2823"
Dim split = value.Split(" "c)
Dim val1 = split(0) ' +1.0
Dim A0 = split(2) ' -256
Dim B0 = split(4) ' -2823
If/When there can be multiple spaces (as in top code sample), your comment about getting rid of them will still allow String.Split
:
Dim value As String = "+1.0 A0 -256 B0 -2823"
Dim tmp = value.Replace(" ", "") ' remove spaces, preserve original just in case
tmp = tmp.Replace("A0", " ") ' replace A0 with space
tmp = tmp.Replace("B0", " ")
Dim split = tmp.Split(" "c) ' split
Dim val1 = split(0) ' grab info
Dim A0 = split(1)
Dim B0 = split(2)
Any character you are sure wont be in the string can be used to replace A0 and B0. Since we just stripped out the spaces, that seems most logical.
Upvotes: 1
Reputation: 27342
You were on the right lines with the first attempt. You just need to use the actual string with the value in to find the location of the items. As you have un potentially unknown number of spaces, you need to useTrim to remove the spaces afterwards:
Dim value As String = "+1.0 A0 -256 B0 -2823" 'this is a line from a txtfile
Dim value1 As String = value.Substring(0, value.IndexOf(" "c))
Dim aPos As Integer = value.IndexOf("A0") 'Find the position of "A0" in the string
Dim bPos As Integer = value.IndexOf("B0") 'Find the position of "B0" in the string
Dim A0 As String = value.Substring(aPos + 2, bPos - (aPos + 2)).Trim
Dim B0 As String = value.Substring(bPos + 2).Trim
Upvotes: 1
Reputation: 455
If you want to split, try Split function ;)
Split(expression as string, delimiter as string) as string()
You want to split your expression by " " and take 1st, 3rd and 5th element from result array (2nd is "A0" and 4th is "B0") So your code should look like :
Option base 0
' so we start counting at 0
Dim results as String()
results = Split(value," ")
value1 = results(0)
A0 = results(2)
B0 = results(4)
Upvotes: 1
Reputation: 16898
You can use String.Split
here with respect to two strings (A0
and B0
), which will be the clearest solution I believe:
Dim value as String = "+1.0 A0 -256 B0 -2823"
Dim values = value.Split({ " A0 ", " B0 " }, StringSplitOptions.None)
Upvotes: 2
Reputation: 8053
Per my comment, using replace and split
String s = "+1.0 A0 -256 B0 -2823";
s = s.Replace("A0", "").Replace("B0", "");
String[] allNumbers = s.Split(' ');
Upvotes: 0