Alex Rivera | Logout

How do I split a string ONLY after the first instance of the delimiter?

Asked 2012-02-27T04:58:13.397
11

I have this code:

strInfo = "3101234567 Ryan Maybach"

Dim varSplit As Variant
varSplit = Split(strInfo, " ")

strPhoneNumber = varSplit(0)
strOwner = varSplit(1)

So, strPhoneNumber = "3101234567" and strOwner = "Ryan"

I want to make it so that strOwner = "Ryan Maybach", the full name, and not just the first name.

How do I split the strInfo string at the first instance of a space " "?

Edit
Report

1 Answer

17

From the MSDN documentation on the Split function:

By default, or when Limit equals -1, the Split function splits the input string at every occurrence of the delimiter string, and returns the substrings in an array. When the Limit parameter is greater than zero, the Split function splits the string at the first Limit-1 occurrences of the delimiter, and returns an array with the resulting substrings.

If you only want to split on the first delimiter then you would specify 2 as the maximum number of parts.

Split(expression, [ delimiter, [ limit, [ compare ]]])
answered 2012-02-27T05:03:41.763

Your Answer