find and replace character from right

Jaspersoft Community 'Read-Only' as of July 8, 2022

Transition to New TIBCO Community Just Weeks Away

You can still search, review wiki content, and review discussions in read-only mode. Please email with questions or issues requiring TIBCO review or response.

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

zaila_1's picture
Joined: Dec 20 2017 - 9:20pm
Last seen: 6 days 19 hours ago

2 Answers:

You can use a regular expression to replace the last comma in a string to "and":


$V{yourString}.replaceFirst(",([^,]+)$", " and$1")

tchen's picture
Joined: Feb 27 2008 - 7:33am
Last seen: 3 days 7 hours ago

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version using JasperReports Library version 6.11.0  -->
<jasperReport xmlns="" xmlns:xsi="" xsi:schemaLocation="" 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>
        <band height="79" splitType="Stretch">
                <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>
                <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>


tchen's picture
Joined: Feb 27 2008 - 7:33am
Last seen: 3 days 7 hours ago