In my personal opinion the cleanest solution is to use regexes. But instead of guessing if it is computationally intensive I would rather benchmark it. Here's the code.
const int Count = 10000000;
const string testString = "<whatever>";
// Solution No. 1: use Regex.Match()
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < Count; i++)
{
var match = Regex.Match(@"\[\s*(\d+)\s*\]$", testString);
if (!match.Success)
continue;
var number = int.Parse(match.Groups[1].Value);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
// Solution No. 2: use IndexOf() and Substring() shenanigans
sw.Start();
for (int i = 0; i < Count; i++)
{
var lb = testString.IndexOf('[');
var rb = testString.LastIndexOf(']');
if (lb < 0 || rb != testString.Length - 1)
continue;
var str = testString.Substring(lb + 1, rb - lb - 1);
int number;
if (!int.TryParse(str, out number))
continue;
// use the number
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
And here are the results:
Solution | testString | Time (ms) | Comment
----------|--------------|--------------|-----------------------
1 | abc [ ] | 4476 | Invalid input string
2 | abc [ ] | 6594 | Invalid input string
1 | abc[1234] | 4446 | Valid input string
2 | abc[1234] | 6290 | Valid input string
As you can see, not only the regex solution is shorter and cleaner, it is actually faster. And if you play with different input strings you will notice that the longer your input string is, the larger the gap between the first and the second solutions.