0

我有一个字符串,我需要用另一个值替换该字符串中的每个字符。该字符串没有分隔符。是否可以遍历该字符串并用一组预定值替换每个值?我想将它保留在一个功能中,因为它将存在于 SSRS 中

Dim stringToChange As String = "123456ALWRYA"

然后我将不得不遍历字符串并用新值替换所有当前值。

即A=001、B=002、1=101、2=102等。

如果字符串没有分隔符,这可能吗?

提前致谢!

4

2 回答 2

1

SSRS 自定义代码的方言非常有限,但这对我有用。

将以下内容添加到报告自定义代码中:

Function SingleReplace (SingleChar As String) As String
  Select Case SingleChar
    Case "A"
      SingleReplace = "001"
    Case "B"
      SingleReplace = "002"
    Case Else
      SingleReplace = SingleChar
  End Select
End Function

Function CustomReplace (BaseString As String) As String

  Dim NewString As New System.Text.StringBuilder

  For Each SingleChar As Char in BaseString
    NewString.Append(SingleReplace(SingleChar))
  Next

  Return NewString.ToString()

End Function

在报告表达式中调用它:

=Code.CustomReplace(Fields!MyString.Value)

在一个简单的报告/表格中为我工作:

在此处输入图像描述

于 2013-10-11T21:52:31.587 回答
0

这基本上是 Styxxy 的建议......使用字典来查找值:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim stringToChange As String = "123456ALWRYA"
    Debug.Print(stringToChange)
    Dim changedString = ConvertString(stringToChange)
    Debug.Print(changedString)
End Sub

Private Function ConvertString(ByVal inStr As String) As String
    Static dict As New Dictionary(Of Char, String)
    If dict.Count = 0 Then
        dict.Add("A"c, "001")
        dict.Add("B"c, "002")
        dict.Add("1"c, "101")
        dict.Add("2"c, "102")
        ' ... etc ...
    End If

    Dim sb As New System.Text.StringBuilder
    For Each c As Char In inStr.ToUpper
        If dict.ContainsKey(c) Then
            sb.Append(dict(c))
        Else
            ' ... possibly throw an exception? ...
            sb.Append(c)
        End If
    Next
    Return sb.ToString
End Function
于 2013-10-11T21:23:15.613 回答