1

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 }

6
  • 2
    string myStringFormat = "I want to surround string {{{0}}} with { and }"; - triple {{{..}}}. Here {{ stands for plain { and the last { is for string interpolation / formatting. Commented Jan 23, 2020 at 15:20
  • Does it have to be { and }? If you would chose another identifier (like [[ and ]]) that's unlikely to appear you could just run string.Replace before handing it over to your string formatting. Commented Jan 23, 2020 at 15:21
  • 1
    Let's me get this straight: you expect the user to write {0} and {a}, but only {0} should be transformed? Commented Jan 23, 2020 at 15:22
  • Bizhan, yes you are right. Commented Jan 23, 2020 at 15:25
  • Use another char Commented Jan 23, 2020 at 15:29

1 Answer 1

1

Use this Regex to find the Argument substrings:

{\d+}

This regex escapes {} {1a} etc. and only chooses {1} {11} etc.


Now you need to handle either Arguments (replace them with their values) or the Escaped curly braces (replace them with double braces). The choice is yours and it depends on the number of occurrences of each case. (I chose to replace arguments in my code below)

Now you need to actually replace the characters. Again the choice is yours to use a StringBuilder or not. It depends on the size of your input and the number of replacements. In any case I suggest StringBuilder.

var m = Regex.Matches(input, @"{\d+}"); if (m.Any()) { // before any arg var sb = new StringBuilder(input.Substring(0, m[0].Index)); for (int i = 0; i < m.Count; i++) { // arg itself sb.Append(args[i]); // from right after arg int start = m[i].Index + m[i].Value.Length; if (i < m.Count - 1) { // until next arg int length = m[i + 1].Index - start; sb.Append(input.Substring(start, length)); } else // until end of input sb.Append(input.Substring(start)); } } 

I believe this is the most robust and cleanest way to do it,and it does not have any performance (memory or speed) issues.


Edit

If you don't have access to args[] then you can first replace {/} with {{/}} and then simply do these modifications to the code:

  1. use this pattern: @"{{\d+}}"
  2. write m[i].Value.Substring(1, m[i].Value.Length - 2) instead of args[i]
Sign up to request clarification or add additional context in comments.

2 Comments

I have tried your code but I am not getting what expected: string input = "I want to append to string {0} these characters { and }"; string[] args = new string[1] { "StringToAppendTo" }; string result = ""; + your code + result = sb.ToString(); the output is StringToAppendTo but the expected is I want to append to string StringToAppendTo these characters { and }
@GiacomoPirinoli I'm dreadfully sorry! I fixed the regex pattern it's working now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.