vba 在 MS Access 中使用 VB 从可变字符串长度中查找子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20000401/
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
Finding a substring from a variable string length using VB in MS Access
提问by Andrew Martin
I have an Access DB with a few tables, one of which has a column of address. These addresses all begin with a building name and are followed by city information. They are all separated by commas.
我有一个带有几个表的 Access DB,其中一个表有一列地址。这些地址都以建筑物名称开头,后跟城市信息。它们都用逗号分隔。
I am looking for a way in the VB code to take this field and separate it into two fields, with everything left of the first comma in one field and everything right of the first comma in the other.
我正在 VB 代码中寻找一种方法来获取此字段并将其分成两个字段,一个字段中第一个逗号左侧的所有内容,另一个字段中第一个逗号右侧的所有内容。
I've tried variations on the following:
我尝试了以下变体:
currentBuildingName = currentBuildingAddress.Substring(0, currentBuildingAddress.IndexOf(","))
currentCity = currentBuildingAddress.Substring(currentBuildingAddress.IndexOf(","), 1)
But with no success. I've also tried looking at the left() and right() functions, but they require you to specify the integer location of where the split should occur, which is variable.
但没有成功。我也试过查看 left() 和 right() 函数,但它们要求您指定应该发生拆分的整数位置,这是可变的。
Any advice folks? How can I split a string based on the first comma, with everything to the left in one field and everything to the right in another field, in MS Access 2010?
大家有什么建议吗?在 MS Access 2010 中,如何根据第一个逗号拆分字符串,一个字段左侧的所有内容和另一个字段右侧的所有内容?
回答by OTTA
Use the InStr()
function, InStr("expression_to_search", "search_string")
. It returns the starting position of the string you're searching for or zero if it doesn't find it. Once you've got the position of the comma you can use Right()
and Left()
to parse the string. For more details see
使用InStr()
函数,InStr("expression_to_search", "search_string")
。它返回您正在搜索的字符串的起始位置,如果没有找到则返回零。一旦获得了逗号的位置,就可以使用Right()
和Left()
来解析字符串。有关更多详细信息,请参阅
回答by Andrew Martin
Thanks to @OTTA for the guidance. This was my final answer:
感谢@OTTA 的指导。这是我的最终答案:
startingPosition = InStr(currentBuildingAddress, ",")
currentBuildingName = Left(currentBuildingAddress, (startingPosition))
currentCity = Right(currentBuildingAddress, Len(currentBuildingAddress) - startingPosition)
Nice and simple.
好看又简单。