zaila_1 Posted March 4, 2022 Share Posted March 4, 2022 Hi, I have string with commas. I want to replace last comma with ' and ' eg: sunday, monday, tuesday -> sunday, monday and tuesday apple, banana, strawberry, lemon -> apple, banana, strawberry and lemon thank you Link to comment Share on other sites More sharing options...
Tom C Posted March 4, 2022 Share Posted March 4, 2022 You can use a regular expression to replace the last comma in a string to "and": $V{yourString}.replaceFirst(",([^,]+)$", " and$1") Link to comment Share on other sites More sharing options...
Tom C Posted March 4, 2022 Share Posted March 4, 2022 <?xml version="1.0" encoding="UTF-8"?><!-- Created with Jaspersoft Studio version 8.0.1.final using JasperReports Library version 6.11.0 --><jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="demo20220303" pageWidth="612" pageHeight="792" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="6a28a568-5cd4-4fc1-9e28-015213d63b58"> <parameter name="Input String" class="java.lang.String"> <defaultValueExpression><![CDATA["apple, banana, strawberry, lemon"]]></defaultValueExpression> </parameter> <queryString> <![CDATA[]]> </queryString> <title> <band height="79" splitType="Stretch"> <textField> <reportElement mode="Opaque" x="0" y="0" width="570" height="30" backcolor="#FFFF00" uuid="e4fc6e91-9a90-4b32-98e2-7355e3b81afe"/> <textFieldExpression><![CDATA["INPUT STRING: "+$P{Input String}]]></textFieldExpression> </textField> <textField> <reportElement mode="Opaque" x="1" y="40" width="570" height="30" backcolor="#33FF00" uuid="323b73b5-269c-42ad-a1ad-a6689316fc8c"/> <textFieldExpression><![CDATA["Modified String: "+$P{Input String}.replaceFirst(",([^,]+)$", " and$1")]]></textFieldExpression> </textField> </band> </title></jasperReport> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now