Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove enclosing brackets from a string in C# [closed]

Tags:

c#

brackets

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.

like image 840
Arun Avatar asked Oct 11 '12 14:10

Arun


People also ask

How to remove brackets from string in C?

Step 1: Declare and read the input at runtime. Step 2: Traverse the string. Step 3: Copy each element of the input string into new string. Step 4: If anyone parenthesis is encountered as an element, replace it with empty space.

How do you remove extra brackets from a string?

Approach: Start traversing from left to right. Check if the element at current index is an opening bracket '(' then print that bracket and increment count. Check if the element at current index is a closing bracket ')' and if the count is not equal to zero then print it and decrement the count.

How do you remove the bracket at the end of a string in Java?

replaceAll("(",""); test = test. replaceAll(")","");

How do I remove extra brackets from a string in Python?

Using strip() to Remove Brackets from the Beginning and End of Strings in Python. If your brackets are on the beginning and end of your string, you can also use the strip() function. The Python strip() function removes specified characters from the beginning and end of a string.


1 Answers

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
like image 129
Nasreddine Avatar answered Oct 21 '22 20:10

Nasreddine