【问题标题】:Finding a substring from a variable string length using VB in MS Access在 MS Access 中使用 VB 从可变字符串长度中查找子字符串
【发布时间】:2013-11-15 11:47:53
【问题描述】:

我有一个带有几个表的 Access DB,其中一个表有一列地址。这些地址都以建筑物名称开头,然后是城市信息。它们都用逗号分隔。

我正在 VB 代码中寻找一种方法来获取该字段并将其分成两个字段,一个字段中第一个逗号左侧的所有内容,另一个字段中第一个逗号右侧的所有内容。

我尝试了以下变体:

currentBuildingName = currentBuildingAddress.Substring(0, currentBuildingAddress.IndexOf(","))
currentCity = currentBuildingAddress.Substring(currentBuildingAddress.IndexOf(","), 1)

但没有成功。我也尝试查看 left() 和 right() 函数,但它们要求您指定拆分应该发生的整数位置,这是可变的。

各位有什么建议吗?在 MS Access 2010 中,如何根据第一个逗号拆分字符串,将左侧的所有内容放在一个字段中,将右侧的所有内容放在另一个字段中?

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    使用InStr() 函数InStr("expression_to_search", "search_string")。它返回您正在搜索的字符串的起始位置,如果找不到,则返回零。一旦你得到了逗号的位置,你就可以使用Right()Left() 来解析字符串。更多详情见

    InStr Function.

    【讨论】:

    • 是的,这就是要走的路。我会将其标记为已接受,但将我的最终结果作为单独的答案发布。谢谢
    【解决方案2】:

    感谢@OTTA 的指导。这是我的最终答案:

    startingPosition = InStr(currentBuildingAddress, ",")
    
    currentBuildingName = Left(currentBuildingAddress, (startingPosition))
    currentCity = Right(currentBuildingAddress, Len(currentBuildingAddress) - startingPosition)
    

    漂亮又简单。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-26
      • 2018-07-02
      • 1970-01-01
      • 2013-05-03
      • 2021-12-27
      • 1970-01-01
      • 2022-08-10
      • 2021-07-24
      相关资源
      最近更新 更多