Is there a way to have a string of numbers be separated by a hyphen instead of commas when dealing with a range? For example instead of it looking like this: 1, 2, 3, 4, 5, 6 ; it should look like this 1-6 on a report.
Thank you!
Is there a way to have a string of numbers be separated by a hyphen instead of commas when dealing with a range? For example instead of it looking like this: 1, 2, 3, 4, 5, 6 ; it should look like this 1-6 on a report.
Thank you!
Hi @kelshorn!
Assuming the string of numbers will be in a Field in SpiReport, you can use the following Java expression in SpiReport to achieve what you are looking for.
While editing any Expression, you can insert
($F{X}.toString().split(",")[0] + "-" + $F{X}.toString().split(",")[$F{X}.toString().split(",").length - 1].trim())
Just replace X
with the name of the field that contains the range in SpiReport!
Please reply if you have any further questions, or if you need the expression refined further!
Things to Note
1, 2, 3 5 6
would display as 1-3 5 6
a, b, c, d
would display as a-d
1, a, b, c, e, 4
would display as 1-4
If youāre curious, about how this expression works, here is a brief explanation
What this expression does is ābreakā and split the range at the ,
character and converts it to a list of elements.
For example, if given the range (which will be a āstringā datatype in Java) 1, 3, 5, 7, 9
we break it into a list with the separate elements 1, 3, 5, 7, 9
. The difference is that the string 1, 3, 5, 7, 9
is one element/structure while the list has 5 separate elements/structures (each being one of the numbers, in the order they appear in the string).
With the elements being separated, we tell SpiReport that we want the very first element in this list, then a -
character and finally the last element in the list.
Thus the display is always first-last
Thank you for the quick response. I tried this expression and it added the range but instead of it being 1-3, itās coming out in Specify as 3-3. Do you know why it could be doing this? I checked to make sure the expression was exactly the same and it is.
Thank you,
Kelsey Minatra
Jason_m,
I figured out what was going on. I needed the expression to be $V since itās a variable for that field.
Thanks so much!
Kelsey Minatra
Iām glad you found a solution to your problem so quickly!