-4

For Example I have String like this:

""dear customer{Customer name} your reference number is {referenceNumber}"

I want to get array=["{Customer name}",{referenceNumber}]"

I have to split based on curly bracket inside bracket value is changeable means it can be different for different cases I just need to split and get array of value inside brackets including brackets.

  • 6
    What have you tried? Please show your effort so far and include details about the actual problem being faced. – tnw May 14 '21 at 18:36
  • i just want to get array=["{Customer name}",{referenceNumber}]" from above given string in question@tnw – blawal sarfraz May 14 '21 at 18:40
  • 1
    I'm aware. Read my comment again please. – tnw May 14 '21 at 18:42
  • 1
    What you are asking for is unlikely to help you to achieve the goal to implement templates... https://stackoverflow.com/questions/39854418/how-to-replace-curly-braces-and-its-contents-in-a-string (and many similar question) show what you should be doing... – Alexei Levenkov May 14 '21 at 18:50
  • thanks, @AlexeiLevenkov the same thing i wanted – blawal sarfraz May 14 '21 at 18:55

1 Answers1

1

If you think about it, splitting on { and } will produce an array where every odd index is what you want..

.Split('{','}').Where((s,i)=>i%2==1).Select(s=>'{' + s + '}').ToArray();

Split the string, use the LINQ Where function that passes the int index to the predicate, insist that the index be odd (mod2 is 1) and select a new string that puts the brackets back on, ToArray

Caius Jard
  • 72,509
  • 5
  • 49
  • 80