-2

I want to get and replace the string between special character. e.g. myString = "hello my first string is {String Name} and my Second string is {String Name}"

I want to replace the string between "{" "}".

Parita
  • 57
  • 6
  • Possible solution is here http://stackoverflow.com/questions/378415/how-do-i-extract-text-that-lies-between-parentheses-round-brackets – Vijay Dec 08 '16 at 10:37
  • could you describe a little more in detail, can the string vary between the {} and what do you want as the replacement? do you want to replace all values with one ? or each has to be individually – Mong Zhu Dec 08 '16 at 10:48

4 Answers4

0

You can do that with a Regex like this:

Regex.Replace(input, "{.*?}", replaceString);

For example:

 string input = "This is the {text}";
 string replace = "content";

 string result = Regex.Replace(input, "{.*?}", replace);

See also How do I remove all HTML tags from a string without knowing which tags are in it?

Community
  • 1
  • 1
Bidou
  • 7,378
  • 9
  • 47
  • 70
0
string content = "{dsdhs},{sdsds}";
List<string> tempList = new List<string>();
Regex topicRegex = new Regex(@"\{(.*?)\}", RegexOptions.Compiled);
foreach (Match item in topicRegex.Matches(content))
                    tempList.Add(item.Value);
Atul Rungta
  • 323
  • 3
  • 8
  • You should add description/comments, Please refer: [How do I write a good answer?](http://stackoverflow.com/help/how-to-answer). This may helps you! – Divyang Desai Jan 08 '17 at 16:19
0

You can use Regex

var input = "User Name {stringToReplace}";
var output = Regex.Replace(input, @" ?\{.*?\}", "NewString");

Console.WriteLine(output);

.netFiddleLink

Anant Dabhi
  • 10,864
  • 3
  • 31
  • 49
0

People are giving regex answers but it sounds more like you're describing string interpolation.

var string1 = "string 1";
var string2 = "string 2";
var myString = $"hello my first string is {string1} and my Second string is {string2}";

For a more detailed explanation: https://msdn.microsoft.com/en-GB/library/dn961160.aspx

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64