Search code examples
sqlssrs-2008reporting

SRSS Expression to split strings excluding specific string


I have the following function (answered by someone on this forum in another question):

Public Function mySplit(ByVal my_field As String) As String
    Dim result As String = ""       
    If my_field is Nothing or my_field = "" Then
        Return result
    End If
    Dim TestArray() As String = my_field.Split(New Char() {" "c})
    Dim word as String
    Dim counter as Integer = 0      
    For Each word In TestArray
        counter = counter +1
        If counter = TestArray.Length Then
            result = result + word
        Else
                result = result + word + vbCrLf 
        End if
    Next
    Return result

End Function

This works to take a collection of words:

"IMAGE01 IMAGE02 IMAGE03" and display them in a report as:

IMAGE01
IMAGE02
IMAGE03

However, I have some images named "IMAGESYS RES01" which because of the above function are being split onto two lines:

IMAGESYS
RES01

I need to avoid this, as the above "IMAGESYS RES01" is just one file.


Solution

  • If it always follows the pattern IMAGESYS RESXX this expression should work:

    =REPLACE(Code.mySplit(REPLACE(Fields!YourField.Value,"S R","S_R")),"_"," ")
    

    With this example IMAGE01 IMAGE02 IMAGE03 IMAGESYS RES01 IMAGESYS RES02 it should return:

    IMAGE01
    IMAGE02
    IMAGE03
    IMAGESYS RES01
    IMAGESYS RES02
    

    UPDATE Explanation about how it is working:

    The split does nothing different, the change is in the way we pass the values to the mySplit function. The REPLACE(Fields!YourField.Value,"S R","S_R") turns your field in this and pass it to the mySplit function:

    IMAGE01 IMAGE02 IMAGE03 IMAGESYS_RES01 IMAGESYS_RES02
    

    The mySplit function will return the below string based on the above passed argument.

    IMAGE01
    IMAGE02
    IMAGE03
    IMAGESYS_RES01
    IMAGESYS_RES02
    

    Once the function returns the above string the outer REPLACE function replace the _ for " " (whitespace), leaving the string as:

    IMAGE01
    IMAGE02
    IMAGE03
    IMAGESYS RES01
    IMAGESYS RES02
    

    Let me know if this helps.