I just wondered what\'s the easiest way to replace a string characters that must be replaced subsequently.
For example:
var str = \"[Hello World]\";
Here's a very uncool way to do it. But it has the advantage of being pretty close to foolproof, I think, and not using regex (in case you'd rather not use regex).
StringBuilder sb = new StringBuilder();
foreach (char c in str.ToCharArray()) {
if (c == '[' || c == ']') {
sb.Append('[' + c + ']');
}
else {
sb.Append(c);
}
}
string result = sb.ToString();