Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern (formatting) for String expression

I am using iReport for designing of reports. I need to format a string such as xxxxxxxxxxx to xxx xxx xxx xxx. I am able to specify pattern for date, number, time and currency.

But how do I do this for a string? Can anyone provide me an insight on this.

like image 679
Nagarajan S R Avatar asked Dec 01 '25 15:12

Nagarajan S R


1 Answers

Try using a regular expression with replacements:

Let's say that your data is called $F{field} and it is a java.lang.String, and the current value is "0123456789ab" and you want it to be "012 345 678 9ab". Try this as your text field value:

$F{field}.replaceAll("^(...)(...)(...)(...)$", "$1 $2 $2 $4")

If you want something more generic, you can get cuter with your regular expression and replacement. For example, if you always want triplets but you don't care how many there are, you can do:

$F{field}.replaceAll("(...)", "$1 ").trim()

Note that the trim will be required because the final triplet will have en extra space appended to it.

like image 190
Christopher Schultz Avatar answered Dec 05 '25 04:12

Christopher Schultz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!