Get the value from between parenthesis c#

Many times you have data in a format that needs to be parsed. One thing I have come across lately is needing to get a value from between parenthesis.

Data: Here is my description and it has a code.(1234)

I need to get the code of 1234 from the data.

string value = "Here is my description and it has a code.(1234)";
int startIndex = value.IndexOf('(');
int endIndex = value.IndexOf(')');
int length = endIndex - startIndex;
if (startIndex > -1 && endIndex > -1)
{
return value.Substring(startIndex + 1, length-1);
}
else   return "There are no parenthesis in the value.";

Leave a Reply