commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Woonsan Ko <woon...@apache.org>
Subject Re: [lang] StrSubstitutor - a dollar sign before a variable
Date Thu, 23 Jul 2015 14:28:07 GMT
Hi Anthony,

Putting '$20.00' into the map is not an option in my use case, so I
tried to use a different escape character. But it doesn't seem to be
working either (another bug?):

    @Test
    public void testReplaceEscapingDollarSign() {
        values.put("amount", "20.00");

        final StrSubstitutor sub = new StrSubstitutor(values);
        sub.setEscapeChar('<');

        String replaceTemplate = "The <${animal} jumps over the ${target}.";
        String expectedResult = "The ${animal} jumps over the lazy dog.";
        String replacedResult = sub.replace(replaceTemplate);
        assertEquals(expectedResult, replacedResult);

        //...
    }

It fails like this:

org.junit.ComparisonFailure: expected:<...uick brown fox paid []$20.00
to jump over ...> but was:<...uick brown fox paid [<]$20.00 to jump
over ...>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at org.apache.commons.lang3.text.StrSubstitutorTest.testReplaceEscapingDollarSign(StrSubstitutorTest.java:182)

I think I'd better file a bug regard to escape character handling.

Regards,

Woonsan



On Wed, Jul 22, 2015 at 9:12 PM, Anthony Brice
<anthonybrice@lateachiever.com> wrote:
> It's not a bug---that's a feature! :p
>
> From the javadoc: "If this character ['$'] is placed before a variable
> reference, this reference is ignored and won't be replaced." So even when
> you use three dollar signs, you still have a variable reference
> ("${amount}") with the escape character placed before it, thus the variable
> reference will not be replaced.
>
> To achieve your desired effect, I think you either have to put the dollar
> sign in the mapping (e.g., "values.put("amount", "$20.00"), use different
> delimiters, or just set a different escape character.
>
> Regards,
> Anthony Brice
>
> On Wed, Jul 22, 2015 at 2:50 PM, Woonsan Ko <woonsan@apache.org> wrote:
>
>> Hi there,
>>
>> I tried to use the following, expecting "...ick brown fox paid $20.00
>> to jump over the la…":
>>
>>     // In org.apache.commons.lang3.text.StrSubstitutorTest.java locally
>>     // after cloning https://github.com/woonsan/commons-lang.
>>     @Test
>>     public void testReplaceEscapingDollarSign() {
>>         values.put("amount", "20.00");
>>         doTestReplace("The quick brown fox paid $20.00 to jump over
>> the lazy dog.",
>>                       "The ${animal} paid $$${amount} to jump over the
>> ${target}.", true);
>>     }
>>
>> (I put double dollar signs like $$${amount} because $ is the default
>> escape character.)
>>
>> But, the result was:"...ick brown fox paid $${amount} to jump over the
>> la…".
>>
>> Is it a bug or did I miss something?
>>
>> Regards,
>>
>> Woonsan
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message