使用三列的自定义代码在SSRS 2008 R2中拆分字符串

时间:2018-08-29 16:52:11

标签: sql sql-server vb.net reporting-services ssrs-2008

我正在分割字符串以分解由|分隔的列。 我想将其分为3列。

  

ATTN:Brian Adams |阿尔伯特大街343号|第十七街

这是我现在拥有的代码

   Public Function GetAddress1 (ByVal a as String)
        Dim b() as string
        b=Split(a,"|")
        Dim str_1(b.Length) As String
        Dim i As Integer
        For i = 1 To b.Length - 1
            str_1(i) = b(i).Split("|")(0)
        Next
   return str_1
End Function


Public Function GetAddress2 (ByVal a as String)
        Dim b() as string
        b=Split(a,"|")
        Dim str_1(b.Length) As String
        Dim i As Integer
        For i = 2 To b.Length - 1
            str_1(i) = b(i).Split("|")(0)
        Next
   return str_1
End Function

Public Function GetAttention (ByVal a as String)
        Dim b() as string
        b=Split(a,"|")
        Dim str_1(b.Length) As String
        Dim i As Integer
        For i = 0 To b.Length - 2
            str_1(i) = b(i).Split("|")(0)
        Next
   return str_1

End Function

只有2个值时,这些功能才能起作用。 ATTN:Brian Adams | 阿尔伯特大街343号,但是当存在三个值时,GetAddress1将在第二个|的右侧获取信息,并获得GetAddress2值。

2 个答案:

答案 0 :(得分:0)

因此,您只需要数组中的前2个元素?

    Dim TheSring As String = "ATTN: Brian Adams | 343 Albert Ave | 17th Street"
    Dim StrAry() As String = TheSring.Split("|")

    For i As Integer = 0 To 1
        TextBoxOutput.Text &= StrAry(i).Trim & Environment.NewLine
    Next

答案 1 :(得分:0)

Public Function GetAddress1 (ByVal a as String)
        Dim b() as string
        b=Split(a,"|",2)
        Dim str_1(b.Length) As String
        Dim i As Integer
        For i = 1 To b.length - 1 
            str_1(i) = b(i).Split("|")(0)
        Next
   return str_1
End Function

写入,2 会在字符串中找到第二个值,而不会引发错误。