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
.
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.
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.
replaceAll("(",""); test = test. replaceAll(")","");
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With