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 community@tibco.com with questions or issues requiring TIBCO review or response.

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

zaila_1's picture
11
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
112969
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 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>

 

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