How do I extract text that lies between parentheses (round brackets)?

Ben H picture Ben H · Dec 18, 2008 · Viewed 345.3k times · Source

I have a string User name (sales) and I want to extract the text between the brackets, how would I do this?

I suspect sub-string but I can't work out how to read until the closing bracket, the length of text will vary.

Answer

Jelly Ama picture Jelly Ama · Aug 27, 2011

If you wish to stay away from regular expressions, the simplest way I can think of is:

string input = "User name (sales)";
string output = input.Split('(', ')')[1];