0

I am trying to extract a sub string from a string using Visual Basic.

This is what I have tried:

Dim TestString = "Aberdeen, 1"
for i = 0 to TestString.length-1
debug.print(teststring(i))
next

What I would like to do is loop through the TestString so that it only extracts and prints the words "Aberdeen" WITHOUT the comma and the number 1. So far, all the characters are getting added.

Any help much appreciated.

2
  • Could TestString have other values? Should it simply stop when it finds a comma? Commented Jan 30, 2020 at 9:24
  • yes it needs to stop the loop at the comma and spit the word Aberdeen out. Commented Jan 30, 2020 at 9:25

1 Answer 1

2

Assuming you are using VB.NET, you can create a LINQ statement which will take characters from the string until (but not including) it finds a comma:

Dim TestString = "Aberdeen, 1"
Dim toComma = TestString.TakeWhile(Function(c) c <> ","c)
For Each c In toComma
    Debug.WriteLine(c)
Next

If you know that there is a comma, it could be:

Dim toComma = TestString.Split({","c}, 2)(0)

Or:

Dim toComma = TestString.Substring(0, TestString.IndexOf(","c))
Sign up to request clarification or add additional context in comments.

1 Comment

This is awesome. Thank you very much. I'm new to VB and have been trying to work this one out. Thanks for your help.

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.