regexreporting-servicesssrs-2012sentencecase

SSRS sentence case with regex


I want to convert a field in a SSRS report to sentence case eg:

some test sentence. second Sentence.

should be converted to:

Some test sentence. Second sentence.

I am currently attempting this using a regular expression:

=System.Text.RegularExpressions.Regex.Replace(
 IIf(IsNothing(Fields!Title.Value), "", LCase(Fields!Title.Value)),
 "(^[a-z])|\.\s+(.)",
 UCase("$1")
)

The above rejex is failing. It seems that the part: UCase("$1") is not working. What I'm getting is the entire string in lowercase.


Solution

  • As mentioned in comments SSRS will not capitalize "$1" identifier since it will take it as literal string.

    As workaround I suggest you use this custom code:

    Go to Report Properties / Code and put this code:

    Function ProperCase(InputString as String) As String
             Dim i as Integer
             If InputString  <> "" Then
                Mid(InputString , 1, 1) = UCase(Mid(InputString , 1, 1))
                For i = 1 To Len(InputString) - 1
                   If Mid(InputString, i, 2) = "." + " " Then
                      Mid(InputString, i + 2, 1) = UCase(Mid(InputString, i + 2, 1))
                   End If
                Next
                Return InputString
             End If
    End Function
    

    To use it invoke it in your textbox as follows:

    =Code.ProperCase(LCase(Fields!Title.Value))
    

    I've tested this string:

    some test sentence. second Sentence. SHOULD THIS BE CAPITALIZED?
    

    It returned:

    Some test sentence. Second Sentence. Should this be capitalized?
    

    Let me know it this can help you.