You should use Regular Expressions -- they're a pretty powerful way to match strings of text against certain patterns, and this is a great scenario in which to use them.
The pattern "\d+
" will match a sequence of 1 or more digits. A simple method that uses this pattern to extract all numbers from a string is as follows:
public static List ExtractInts(string input)
{
return Regex.Matches(input, @"\d+")
.Cast()
.Select(x => Convert.ToInt32(x.Value))
.ToList();
}
So you could use it like this:
List result = ExtractInts("( 01 - ABCDEFG )");
For some more detailed info on Regular Expressions, see this page (MSDN) or this page (helpful "cheat sheet").