我有一个字符串,我需要用另一个值替换该字符串中的每个字符。该字符串没有分隔符。是否可以单步执行该字符串并用一组预定值替换每个值?我想将它保留在一个函数中,因为它将存在于SSRS中
Dim stringToChange As String = "123456ALWRYA"
然后我必须逐步执行字符串并用新的值替换当前值。
即。 A = 001,B = 002,1 = 101,2 = 102等。
如果字符串没有分隔符,这可能吗?
提前致谢!
答案 0 :(得分: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)
在一份简单的报告/表格中为我工作:
答案 1 :(得分: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