function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
springdevspringdev 

How to escape braces in a string format.

I have a use case where I need to do some string substitution on a string that has brace characters {}.  What is the proper way to escape the braces to make this test pass?

 

    public static testMethod void StringFormatFailsWithBraces()
    {
        List<string> substitutions = new List<String>();
        substitutions.add('world');
        string result = string.format('{hello {0}}', substitutions);
        System.assertEquals('{hello world}', result);
    }

 

Below is the very interesting exception which leads to demo.icu-project.org, which doesn't tell me any information.


System.StringException: All argument identifiers have to be either non-negative numbers or strings following the pattern ([:ID_Start:] [:ID_Continue:]*).
For more details on these unicode sets, visit http://demo.icu-project.org/icu-bin/ubrowse

 

Thanks!

 

Matt

Best Answer chosen by Admin (Salesforce Developers) 
Saurabh DhobleSaurabh Dhoble

Finally got it to work. You have to put the braces in the Format List :-

 

            List<String> subs = new List<String>();
            subs.add('{');
            subs.add('World');
            subs.add('}');
            return String.format('{0}Hello {1}{2}', subs);

 

All Answers

Saurabh DhobleSaurabh Dhoble

Try this :-

 

string result = string.format('\{hello {0}\}', substitutions);

springdevspringdev

Thanks, I had tried this.  But this produces a different error "Invalid string literal '\{helllo {0}\}'. Illegal character sequence '\{' in string literal.

Saurabh DhobleSaurabh Dhoble

Finally got it to work. You have to put the braces in the Format List :-

 

            List<String> subs = new List<String>();
            subs.add('{');
            subs.add('World');
            subs.add('}');
            return String.format('{0}Hello {1}{2}', subs);

 

This was selected as the best answer
springdevspringdev

Thanks!  I'm sure that would work, but it doesn't actually help my situation. 

Jerun JoseJerun Jose
Im not very familiar with the string.format function. But from what I can understand, probably you use the replaceAll with regex help to replace the '{' character with some other unique string. Do the same for the '}' character. Now your new string will not have { and } except where it uses the format string placeholder. After you apply the format, you can then resubstitute that unique string for { and }. Rough, but hope it helps..
springdevspringdev

This is exactly what I did.  A bunch of string constants to escape out the ones that are part of a format and the ones that are not part of a format.  Then some well placed string.replace functions.

 

If salesforce were looking for ideas on how better to handle this, I highly recommend what C# has done, which is shown below: 

            Assert.That(string.Format("{{hello {0}}}", "world"), Is.EqualTo("{hello world}"));