public static void Main()
Console.WriteLine(GetValueBetweenSquareBrackets("001"));
Console.WriteLine(GetValueBetweenSquareBrackets("001["));
Console.WriteLine(GetValueBetweenSquareBrackets("001]"));
Console.WriteLine(GetValueBetweenSquareBrackets("001[002]"));
public static string GetValueBetweenSquareBrackets(string brokerCode)
int startIndex = brokerCode.IndexOf('[');
int endIndex = brokerCode.IndexOf(']');
if (startIndex != -1 && endIndex != -1 && endIndex > startIndex)
return brokerCode.Substring(startIndex + 1, endIndex - startIndex - 1);