Question

  Asked: Mar 14 2008   8:54 AM GMT
  Asked by: Jothi1


How to Split a String using Visual Basic


Visual Basic, Strings

I want to Split the String
sMemberName=Totalmarket <null> <null> C10A and store it separately
how can i do it using Visual Basic

Subscribe to Alerts! Get questions and answers delivered to your Inbox.


E-mail me updates on this question



   SUBSCRIBE

hidden modal window

Answer Wiki (Improve, edit or add to this answer)


 RATE THIS ANSWER
0
Click to Vote:
  •   0
  •  0



I'm not sure I quite understand the question, but you can use the Left(), Right() or Mid() functions to get a subset of a given string taken from the left, right or middle of a string.

Dim subString as String
subString = Left("One two three", 3) ' subString = "One"
subString = Right("One two three", 5) 'subString = "three"
subString = Mid("One two three", 5, 3) 'subString = "two"
subString = Mid("One two three", 5) 'subString = "two three"


You can use InStr() or InStrRev() to get the first or last occurrence of one string in another. So your end code may be something like:

Dim bigString as String
Dim leftString as String
Dim rightString as String
Dim splitAt as Integer

bigString = "hello world"

splitAt = InStr(bigString, "world") ' splitAt = 7
leftString = Left(bigString, splitAt-1) ' leftString = "hello "
' (note the space after "hello")
rightString = Mid(bigString, splitAt) ' rightString = "world"
  • AddThis Social Bookmark Button

Browse more Questions and Answers on Development.

Looking for relevant Development Whitepapers? Visit the SearchWinDevelopment.com Research Library.


Discuss This Answer


You must be logged-in to discuss a question. Log-in/Register

BobBeechey  |   Mar 15 2008  3:45AM GMT

In flavours of visualo basic .NET you can use the Split function. It normally splits a string into substrings based on the space character as separator but, assuming your <null> implies the need to use chr(0) as separator, this is easily done.
The following code gives the hint:
Dim TheString As String = "First" + vbNullChar.ToString() + "Thing"
Dim TheArray As String() = Split(TheString, vbNullChar)
TheArray.Length will now, correctly, be 2 and TheArray(0) will be “First” and TheArray(1) will be “Thing”.
The use of two <null> consecutively as in your answer will lead to production of an intervening empty string(string of length zero) in the output array.