-1

I dont know is that duplicate or not,but i not found same(maybe i not found because it got hard title)

So,i have a this string:

string a = "(Hello(World),World(Hello))";

And i need to remove a first Bracket,and last Bracket. And get that output:

Hello(World),World(Hello)

I not need to remove first char and last. I need to remove first specific char(bracket) and last specific char(close bracket).

That says,if string is be:

string a = "gyfw(Hello(World),World(Hello))";

Output is be: gyfw Hello(World),World(Hello)

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Qrai
  • 31
  • 1
  • 6
  • See [here](https://stackoverflow.com/questions/42964150/how-to-remove-first-and-last-character-of-a-string-in-c): a = a.Substring(1, a.Length - 2); – alxnull Sep 15 '18 at 18:28
  • We know the bracket is the last one, so `a.Substring(0, a.Length - 1)` will remove the last one. For the second part, a simple google search yielded: https://stackoverflow.com/questions/141045/how-do-i-replace-the-first-instance-of-a-string-in-net – Blue Sep 15 '18 at 18:35
  • quanik,Nope,i not need to remove first and last char,like `(hello)` to `hello`. I need to remove first bracket and last bracket,like `a(hi);` to `a hi ;` – Qrai Sep 15 '18 at 18:36
  • FrankerZ,okay that ok to Replace First Bracket.Can explan how to remove last? – Qrai Sep 15 '18 at 18:38

3 Answers3

1

To remove first specific char:

a = a.Remove(a.IndexOf("("), 1);

To remove last specific char:

a = a.Remove(a.LastIndexOf(")"), 1);
Qrai
  • 31
  • 1
  • 6
alxnull
  • 909
  • 9
  • 18
0

In a balanced way, it can be done with this regex

Find @"\(((?>[^()]+|\((?<Depth>)|\)(?<-Depth>))*(?(Depth)(?!)))\)"
Replace @"$1"

If it is required to have inner parens change the * to a +.
If it is required that it should only match once and span the string, add ^ and $ to beginning / end respectively to the regex.

Here is the regex explained

 \(                            #  Match ( a open parenth
 (                             # (1 start), Capture the core, to be written back

      (?>                           # Then either match (possessively):

           [^()]+                        # Any character except parenths
        |                              # or
           \(                            # Open (  increase the paren counter
           (?<Depth> )
        |                              # or
           \)                            # Close )  decrease the paren counter
           (?<-Depth> )
      )*                            # Repeat as needed.
      (?(Depth)                     # Assert that the paren counter is at zero.
           (?!)
      )

 )                             # (1 end)
 \)                            # Match ) a closing parenth 
  • 1
    Im so nub to Regex Can u show code with find and replace functions Regex.Find ? Regex.Replace ? – Qrai Sep 15 '18 at 18:52
  • @Qrai - Since you've been a member for 8 months, I'm sure you know that SO is not a code writing service. So, sorry, no _code_ for you. I can give you a link to C# overview where you can navigate to code examples. –  Sep 17 '18 at 19:19
-1

Use the String.Substring() method to remove specific character.

So, if your string is stored in a variable myval:

myval = myval.Substring(1, myval.Length - 1);
Blue
  • 22,608
  • 7
  • 62
  • 92
Akshay Jain
  • 790
  • 1
  • 7
  • 21