【问题标题】:How do I split one number from another to populate a field如何将一个数字与另一个数字分开以填充字段
【发布时间】:2016-10-02 21:59:28
【问题描述】:

我有一个格式为“24849_1”的 VendorID。我正在尝试将“1”自己分开并用它填充一个字段。以下代码是我正在使用的,但我要做的就是完全去掉“1”。有人有什么建议吗?

Dim strCustom1 As String
Dim strVendorID As String 
strCustom1 = pWorkdoc.Fields("VendorID").Text
If strCustom1 <> "" Then
   strCustom1 = Split(strCustom1, "_") (0)
   xmlDoc.getElementsByTagName("RemitTo").Item(0).Text = strCustom1
End If

【问题讨论】:

    标签: vb.net split trim truncate


    【解决方案1】:

    你打电话给Split,但你真的费心去了解它的作用吗?它返回一个包含拆分部分的数组。然后,您将获得该数组中索引 0 处的部分。如果你真的想要索引 1 的部分,即第二部分,那么你应该使用索引 1 而不是索引 0。

    【讨论】:

    • 我知道它返回了一个数组,与我需要的索引差不多。谢谢你的回答,很有帮助。
    【解决方案2】:

    Split 会给你一个数组,你只取第一个元素;你可以做的是:

    strCustom1 = pWorkdoc.Fields("VendorID").Text // let it be 24849_1
    If strCustom1 <> "" Then
       Dim resultArray = Split(strCustom1, "_")
       //resultArray[0] will be  24849
       //resultArray[1] will be  1
       //assign them as you need
    End IF
    

    【讨论】:

      【解决方案3】:

      Split 函数返回一个字符串数组,但您只取了第一个元素(包含“1”的第二个元素被丢弃)。

      将拆分数组保存在一个变量中,以便您可以访问这两个元素。

          Dim strCustom1 As String = "24849_1"
          Dim split As String()
          split = strCustom1.split("_")
      
          Console.WriteLine("Part1={0} Part2={1}", split(0), split(1))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-16
        • 2016-12-22
        • 2021-10-05
        • 2019-06-12
        相关资源
        最近更新 更多