Search code examples
reporting-servicesssrs-2008ssrs-2008-r2

Start a new line after exactly two words of a string in SSRS-2008


I have this string that is coming from the db and the words are separated by a space. the number of words can vary from a minimum of one to a maximum of six, so what i essentially want is to start a new line immediately after the second word. For example the string "Natural Financial Services" needs to show as:expected result in the report. i case if there is only one word then there should not be any line break, i have tried this Replace(Fields!CustodianNameTxt.Value," ",Vbcrlf) but this will cause each word of the string to appear in a separate line, like this:result with my current expression Which is not what is expected, anyone please suggest if there is any solution to achieve this? Thanks in advance.


Solution

  • Select report properties -> Code

    Add this function (there might be better ways of achieving the same in VB, this is just one example):

    Public Function splitline(byval line as string) as string   
    
    dim words() as string=line.split(" ") ' separate the lines into array of words
    dim pos as integer ' to calculate the position for the breaks
    dim newlines as string = line ' string for the line with added breaks
    
    if words.length > 2 then  ' there are 3 or more words add break after second word
        pos=len(words(0)) + len(words(1)) + 1 ' this will be the position of the first break
        newlines=newlines.remove(pos,1).insert(pos,VBCRLF) ' remove the space and add a break
    end if
    
    if words.length > 4 then  ' there are 5 or more words add break after forth word
        pos=len(words(0)) + len(words(1)) + len(words(2)) + len(words(3)) + 4 ' adding 4 because 2 spaces + cr + lf
        newlines=newlines.remove(pos,1).insert(pos,VBCRLF) ' remove the space and add a break
    end if  
    
    return newlines
    
    End Function
    

    The expression in the will then just be:

    Code.splitline(Fields!CustodianNameTxt.Value)