Search code examples
stringcrystal-reportscrystal-reports-2011

Crystal Reports cut of string


I'm new to Crystal Reports and I'm looking for a way to cut off a string if the string is too long and replace the end with ...

So I'm using Crystal Reports to generate Word or PDF Files. The Problem now is that I have a textfield that is too small for long strings.

The thing I want to do is check if the string is too long for the field. If it is, cut the string at the last , and replace the end with ....

How and where can I do that? How would the code look like?

I am using Crystal Reports 2011.


Solution

  • One way is to use the Len-, Left-, Instr- and Instrrev-functions.

    The following formula should give you the expected result. (Only works with a monospaced font.)

    NumberVar MaxLen := 200;
    
    If Len({MyTable.MyColumn}) > MaxLen Then
        //Length is > MaxLen
        If Instr(Left({MyTable.MyColumn}, MaxLen),",")>0 Then
            //comma (,) found in first 200 chars -> cut at last comma
            Left({MyTable.MyColumn}, Instrrev(Left({MyTable.MyColumn}, MaxLen), ",")-1) & "..."
        Else    
            //NO comma (,) found in first 200 chars -> cut after char 197
            Left({MyTable.MyColumn}, MaxLen-3) & "..."
    Else
        //Length is <= 200  -> use the whole text
        {MyTable.MyColumn}
    

    In this example, the maximum length is 200 chars.
    Just adjust the variable MaxLen to the number of chars you need.