在 SSRS-2008 中的字符串的两个单词之后开始一个新行
Start a new line after exactly two words of a string in SSRS-2008
我有这个来自数据库的字符串,单词之间用 space 分隔。单词的数量可以从最少一个到最多六个不等,所以我基本上想要的是在第二个单词之后立即开始一个新行。
例如字符串 "Natural Financial Services" 需要显示为:expected result
在报告中。如果只有一个单词,那么不应该有任何换行符,我试过这个 Replace(Fields!CustodianNameTxt.Value," ",Vbcrlf)
但这会导致字符串的每个单词出现在单独的行中,如下所示:result with my current expression
这不是预期的,任何人都请建议是否有任何解决方案来实现这一目标?
提前致谢。
一种方法是将字符串拆分为单词数组,然后使用选择函数使用添加的 CRLF 重建它
在这个例子中,choose are inside an isnothing 以满足少于六个单词的行:
=IIF(not isnothing(choose(1,split(Fields!CustodianNameTxt.Value," ")))," " + choose(1,split(Fields!CustodianNameTxt.Value," ")),"")
+IIF(not isnothing(choose(2,split(Fields!CustodianNameTxt.Value," ")))," " + choose(2,split(Fields!CustodianNameTxt.Value," ")) + Vbcrlf,"")
+IIF(not isnothing(choose(3,split(Fields!CustodianNameTxt.Value," ")))," " + choose(3,split(Fields!CustodianNameTxt.Value," ")),"")
+IIF(not isnothing(choose(4,split(Fields!CustodianNameTxt.Value," ")))," " + choose(4,split(Fields!CustodianNameTxt.Value," ")) + Vbcrlf,"")
+IIF(not isnothing(choose(5,split(Fields!CustodianNameTxt.Value," ")))," " + choose(5,split(Fields!CustodianNameTxt.Value," ")),"")
+IIF(not isnothing(choose(6,split(Fields!CustodianNameTxt.Value," ")))," " + choose(6,split(Fields!CustodianNameTxt.Value," ")),"")
Select 报告属性 -> 代码
添加此功能(在 VB 中可能有更好的方法实现相同功能,这只是一个示例):
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
will 中的表达式将是:
Code.splitline(Fields!CustodianNameTxt.Value)
我有这个来自数据库的字符串,单词之间用 space 分隔。单词的数量可以从最少一个到最多六个不等,所以我基本上想要的是在第二个单词之后立即开始一个新行。
例如字符串 "Natural Financial Services" 需要显示为:expected result
在报告中。如果只有一个单词,那么不应该有任何换行符,我试过这个 Replace(Fields!CustodianNameTxt.Value," ",Vbcrlf)
但这会导致字符串的每个单词出现在单独的行中,如下所示:result with my current expression
这不是预期的,任何人都请建议是否有任何解决方案来实现这一目标?
提前致谢。
一种方法是将字符串拆分为单词数组,然后使用选择函数使用添加的 CRLF 重建它 在这个例子中,choose are inside an isnothing 以满足少于六个单词的行:
=IIF(not isnothing(choose(1,split(Fields!CustodianNameTxt.Value," ")))," " + choose(1,split(Fields!CustodianNameTxt.Value," ")),"")
+IIF(not isnothing(choose(2,split(Fields!CustodianNameTxt.Value," ")))," " + choose(2,split(Fields!CustodianNameTxt.Value," ")) + Vbcrlf,"")
+IIF(not isnothing(choose(3,split(Fields!CustodianNameTxt.Value," ")))," " + choose(3,split(Fields!CustodianNameTxt.Value," ")),"")
+IIF(not isnothing(choose(4,split(Fields!CustodianNameTxt.Value," ")))," " + choose(4,split(Fields!CustodianNameTxt.Value," ")) + Vbcrlf,"")
+IIF(not isnothing(choose(5,split(Fields!CustodianNameTxt.Value," ")))," " + choose(5,split(Fields!CustodianNameTxt.Value," ")),"")
+IIF(not isnothing(choose(6,split(Fields!CustodianNameTxt.Value," ")))," " + choose(6,split(Fields!CustodianNameTxt.Value," ")),"")
Select 报告属性 -> 代码
添加此功能(在 VB 中可能有更好的方法实现相同功能,这只是一个示例):
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
will 中的表达式将是:
Code.splitline(Fields!CustodianNameTxt.Value)