vba 如何仅在分隔符的第一个实例之后拆分字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9460240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:20:50  来源:igfitidea点击:

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

stringvbasplit

提问by phan

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"

所以, strPhoneNumber = "3101234567" 和 strOwner = "Ryan"

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

我想让 strOwner = "Ryan Maybach",全名,而不仅仅是名字。

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

如何在空格“”的第一个实例处拆分 strInfo 字符串?

回答by Stephen Tetreault

From the MSDN documentation on the Split function:

从关于拆分功能的 MSDN 文档:

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.

默认情况下,或当 Limit 等于 -1 时,Split 函数在每次出现分隔符字符串时拆分输入字符串,并返回数组中的子字符串。当 Limit 参数大于零时,Split 函数在分隔符的第一个 Limit-1 出现处拆分字符串,并返回一个包含结果子字符串的数组。

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

如果您只想在第一个分隔符上拆分,那么您可以指定 2 作为最大部分数。

Split(expression, [ delimiter, [ limit, [ compare ]]])