How to Split a String using Visual Basic

55 pts.
Tags:
Strings
Visual Basic
I want to Split the String sMemberName=Totalmarket <null> <null> C10A and store it separately how can i do it using Visual Basic
ASKED: March 14, 2008  8:54 AM
UPDATED: March 15, 2008  3:45 AM

Answer Wiki

Thanks. We'll let you know when a new response is added.

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.

<pre>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”</pre>

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:
<pre>
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”</pre>

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • BobBeechey
    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.
    750 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following