简体   繁体   English

摆脱任何括号或括号SSRS中包含的值

[英]Get rid of any parenthesis or values enclosed in parenthesis SSRS

I'm trying to remove any parenthesis or value enclosed in parenthesis in a string field in SSRS, which will be the best way to do it? 我正在尝试删除SSRS的字符串字段中括号内的任何括号或值,这将是最好的方法? I read about replace function but not sure how to use it with several values. 我阅读了有关replace函数的信息,但不确定如何将其与多个值一起使用。

Here are some examples about what I'm trying to do: 以下是一些有关我要执行的操作的示例:

Original string    Expected string
ERP(123)           ERP
TE(PO)ST           TEST
(123)string        string

Assuming you only have a single set of parentheses then this will work. 假设您只有一组括号,那么它将起作用。

=LEFT(Fields!test.Value, InStr(Fields!test.Value, "(") -1)
&
MID(Fields!test.Value, InStr(Fields!test.Value, ")") +1)

Basically we take the left part of the string up to the first "(" position minus 1 and then concatenate the remainder of the string starting at the first ") +1 基本上,我们将字符串的左部分移到第一个“(”位置减去1,然后将字符串的其余部分从第一个“)+1连接起来

For this I would use a function that would be usable everywhere and not always copy paste: 为此,我将使用一个可在任何地方使用并且并不总是复制粘贴的函数:

In here we do a Substring of the part we want to delete, and then we do a Replace to changed with no spaces and that way the string will be together (in the case if the original was separated by the parenthesis) 在这里,我们对要删除的部分执行Substring ,然后对Replace进行Replace (不带空格),这样字符串就可以在一起了(如果原始内容用括号隔开)

Public Function removeParenthesis (input As Object) As Object
    Try
        Dim FirstIndex As Integer = input.IndexOf("(")
        Dim SecondIndex As Integer = input.IndexOf(")") - FirstIndex + 1
        Dim Result As String = input.ToString().Replace(input.Substring(FirstIndex, SecondIndex), "")
        Return Result
    Catch ex As Exception
        Return ex
    End Try
End Function

This is pasted in the Code sections and will be called like this: 它将粘贴在“代码”部分中,并将这样称呼:

Code.removeParenthesis(Fields!Textbox1.Value)

The function is implemented on Report > Report Properties > Code > paste the function above 该功能在“报告”>“报告属性”>“代码”>“粘贴”上面的函数上实现

EDIT Taking in mind what @Alan Schofield said in his answer 编辑考虑到@Alan Schofield在回答中说了什么

Assuming you only have a single set of parentheses [...]. 假设您只有一组括号[...]。

I have made a little change and have validate too if there's actually some parenthesis, the idea is to loop till everything is clean. 我进行了一些更改,并且也进行了验证(如果确实有括号),该想法是循环直到一切都干净为止。

If there's no parenthesis then It'll return original value 如果没有括号,它将返回原始值

Public Function removeParenthesis(input As Object) As Object
        Try
            Dim Result As String = input.ToString()
            Dim FirstIndex As Integer = Result.IndexOf("(")
            Dim SecondIndex As Integer = Result.IndexOf(")")
            While FirstIndex <> -1 And SecondIndex <> -1
                SecondIndex = SecondIndex - FirstIndex + 1
                Result = Result.Replace(Result.Substring(FirstIndex, SecondIndex), "")
                FirstIndex = Result.IndexOf("(")
                SecondIndex = Result.IndexOf(")")
            End While

            Return Result
        Catch ex As Exception
            Return ex
        End Try
    End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM