Sunday, August 14, 2011

[Code] Visual Basic - Stripping Strings

In this example, I will show you how to strip a substring from a source string. The string I will be using is "Leftstring : Rightstring". In this case we will be getting Rightstring.


First, (optionally), you get the full string length using Len. This returns an integer.


Len(myString)


Then, you would use InStr to find the location of the colon. This ALSO returns an integer.


InStr(1, myString, ":")


Now we will run the Mid function with simple maths to return the RightString. We use colonPosition+2 to return the string AFTER the space, not including it.


Mid(myStr, (colonPosition+2), Len(myString))


Similarly, to return Leftstring, we would just use Mid slightly differently.


Mid(myStr, 1, (colonPosition-2))

No comments:

Post a Comment