// first, get the name of the file without the extsension string filename = System.IO.Path.GetFileNameWithoutExtension(@"c:\somePath\x_y 1-2-3_2.txt"); // you should now have "x_y 1-2-3_2" as the file name // find the last underscore int pos = filename.LastIndexOf("_"); // retrieve all of the data after the underscore string part = ""; if (pos >= 0 && pos <= filename.Length -1) { part = filename.Substring(pos); } // parse it into an integer int value; if (Int32.TryParse(part, out value)) { // do something with value... } else { // error }
string s = "Bank_Transactions 2011-07-17_2.txt"; s.SubString(s.LastIndexOf('_') +1); s = s.Replace(".txt", String.Empty);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)