MrPatterns
MrPatterns

Reputation: 4434

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

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 " "?

Upvotes: 11

Views: 26253

Answers (1)

Stephen Tetreault
Stephen Tetreault

Reputation: 787

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 ]]])

Upvotes: 17

Related Questions