starting from this C# example:
string myStringFormat = "I want to surround string {0} with {{ and }}"; string myStringArgs = "StringToSurround"; string myFinalString = string.Format(myStringFormat, myStringArgs); I'd like to know if there is a quick and simple way to distinguish between escape character/sequence and arguments for curly braces/brackets.
The reasons why I am asking this are:
+] I want to provide some logging functionality and I don't want to require users to be aware of the double curly braces/brackets escape rule
+] I want to be very fast in doing this distinction for performance requirements
Currently the only solution I can think about is to scan the string looking for curly braces/brackets and do some check (number parsing) on subsequent characters. Probably regex can be helpful but I cannot find a way to use them in this scenario.
Btw, the final situation I'd like to achieve is user being allowed to this without getting exceptions:
string myStringFormat = "I want to surround string {0} with { and }"; string myStringArgs = "StringToSurround"; //string myFinalString = string.Format(myStringFormat, myStringArgs); throwing exception string myFinalString = MyCustomizedStringFormat(myStringFormat, myStringArgs); EDIT:
sorry the word "surround" was tricky and misleading, please consider this example:
string myStringFormat = "I want to append to string {0} these characters {{ and }}"; string myStringArgs = "StringToAppendTo"; string myFinalString = string.Format(myStringFormat, myStringArgs); giving output
I want to append to string StringToAppendTo these characters { and }
string myStringFormat = "I want to surround string {{{0}}} with { and }";- triple{{{..}}}. Here{{stands for plain{and the last{is for string interpolation / formatting.{and}? If you would chose another identifier (like[[and]]) that's unlikely to appear you could just runstring.Replacebefore handing it over to your string formatting.