[
https://issues.apache.org/jira/browse/LANG-1158?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14639447#comment-14639447
]
Woonsan Ko commented on LANG-1158:
----------------------------------
Anthony gave a good explanation about what the escape character means and how I
can circumvent the issue in the user@ list [1]:
{noformat}
The escape character just tells StrSubstitutor "Ignore the following
variable reference." When you change the default escape character, you
don't need to use it get a dollar sign before a variable reference in your
interpolated string. Try the following:
@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);
replaceTemplate = "The ${animal} paid $${amount} to jump over
the ${target}.";
expectedResult = "The quick brown fox paid $20.00 to jump over
the lazy dog.";
replacedResult = sub.replace(replaceTemplate);
assertEquals(expectedResult, replacedResult);
}
{noformat}
{noformat}
Sorry, the second sentence in the first paragraph should read: "When you
change the default escape character, you don't need to use it _to_ get a
dollar sign before a variable reference in your interpolated string."
To explain a little more, consider what happens to " $${amount}" when
you're using StrSubstitutor's default escape character. StrSubstitutor sees
that you've got a variable reference ("${amount}"), and that you've got the
escape character ("$") before it, so it replaces "$${amount}" with
"${amount}".
Now consider what happens to " $${amount}" when you've set StrSubstitutor's
escape character to something other than the default. StrSubstitutor sees
the variable reference ("${amount}"), notes that the character ("$") before
the variable reference is _not_ the user-defined escape character, and so
replaces "${amount}" with the appropriate value form the map.
{noformat}
I think I can live with using a different escape character for my use case. So
I'll close this issue.
[1] http://markmail.org/message/6p74g52hxkdaswtv
> StrSubstitutor - escape character not working just before a variable
> --------------------------------------------------------------------
>
> Key: LANG-1158
> URL: https://issues.apache.org/jira/browse/LANG-1158
> Project: Commons Lang
> Issue Type: Bug
> Affects Versions: 3.4
> Reporter: Woonsan Ko
>
> I tried to use the following, expecting "...ick brown fox paid $20.00 to jump
> over the la…":
> {code}
> // 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);
> }
> {code}
> {noformat}
> (I put double dollar signs like $$${amount} because $ is the default escape
> character.)
> Because I put three dollar signs in total, it should give one dollar sign
> followed by the resolved amount value: "$20.00".
> But, the result was:"...ick brown fox paid $${amount} to jump over the la…".
> {noformat}
> I also tried using a different escape character like this:
> {code}
> @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);
> replaceTemplate = "The ${animal} paid <$${amount} to jump over the
> ${target}.";
> expectedResult = "The quick brown fox paid $20.00 to jump over the
> lazy dog.";
> replacedResult = sub.replace(replaceTemplate);
> assertEquals(expectedResult, replacedResult);
> }
> {code}
> However, it fails like this:
> {noformat}
> Tests run: 41, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.048 sec
> <<< FAILURE! - in org.apache.commons.lang3.text.StrSubstitutorTest
> testReplaceEscapingDollarSign(org.apache.commons.lang3.text.StrSubstitutorTest)
> Time elapsed: 0.009 sec <<< FAILURE!
> 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)
> {noformat}
> {noformat}
> The second assertion failed. So, it seems working in case of "<${animal}",
> but not working in case of "<$${amount}".
> {noformat}
> I think escape character handling is inconsistent.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)