One way to approach this (other than RegEx) is to write a String Extension method:
public static class StringExtensions
{
public static string FromNextToLastToLast(this string source, string delimiter)
{
int last = source.LastIndexOf(delimiter);
int nexttolast = source.Substring(0, last).LastIndexOf(delimiter);
if (last == 0) return String.Empty;
return source.Substring(nexttolast + 1, last - nexttolast - 1);
}
}
Test:
List<string> testStrings = new List<string>
{
" 01/19/2016 110056234",
"01/19/2016 110056234 283 4",
"Jan 11 2016 3:01PM 110056234 283 4",
"01/19/2016 11234 283 4",
"01/19/2016 110056234 455 43"
};
private void button2_Click(object sender, EventArgs e)
{
string result;
foreach (string str in testStrings)
{
result = str.FromNextToLastToLast(" ");
Console.WriteLine("source: \"{0}\" : result: \"{1}\"",
str,
(result == string.Empty) ? "String.Empty" : result);
}
}
source: "01/19/2016 110056234" : result: "String.Empty"
source: "01/19/2016 110056234 283 4" : result: "283"
source: "Jan 11 2016 3:01PM 110056234 283 4" : result: "283"
source: "01/19/2016 11234 283 4" : result: "283"
source: "01/19/2016 110056234 455 43" : result: "455"