0

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"))
3
  • it appears as if they are all seperated by a space. So use the replace function to replace A0 and B0 with no spaces then split the string on the space char giving you each element. Commented Nov 3, 2014 at 14:51
  • 4
    Just split your string by space and fetch the item in index 0, 2, 4 ignoring index 1 and 3. Commented Nov 3, 2014 at 14:53
  • the spaces are different Commented Nov 3, 2014 at 14:55

5 Answers 5

2

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

the spaces are different but i like your method and i just gonna replace all spaces replace(" ","") and then I replace A0 with a space and B0 with a space and it should work thx :)
the question kind of changed with the edits, I was going by +## A0 #### B0 ### which appears like there is one space between them
sorry it was my fault :)
1

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)

Comments

1

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

Comments

0

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(' ');

3 Comments

if you downvote it you should give a reason as the answer is valid
I didn't downvote, but you should assign the result of the Replace() calls back to s.
ya, was just a typeo, fixed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.