Q:

Remove enclosing brackets from a string in c#

I need to remove enclosing brackets from a string in c# in code behind.
For example, if I have a string as [My [] Groups], I want to turn it into My [] Groups.
Thanks.

A:

The simplest way of addressing this using the string from your example would be taking a substring:
if (s.Length > 2) {
    s = s.Substring(1, s.Length-2);
}

This works only when you are 100% certain that the first and the last characters are indeed square brackets. If they are not, for example, when the string is untrimmed, you may need to perform additional string manipulations (e.g. trimming the string).

A:

Try this:
yourString = yourString.Replace("[", string.Empty).Replace("]", string.Empty);

Updated answer since the question was edited:
string s = "[My [] Groups]";
string pattern = @"^(\[){1}(.*?)(\]){1}$";
Console.WriteLine(Regex.Replace(s, pattern, "$2")); // will print My [] Groups

A:

Simple to use Trim:
var result = "[My [] Groups]".Trim('[', ']');

