You can use a regular expression for that.
Something like
using System.Text.RegularExpressions;
private static readonly Regex r = new Regex(@"_.*", RegexOptions.Compiled | RegexOptions.CultureInvariant);
string filename = "B1_1-A.dwg";
filename = r.Replace(filename, "");
Note: if you are using the regular expression repeatedly, it is probably best to make it a static readonly field in the class you are using it, because creating a regex repeatedly impacts the expected performance; if you make it static, the performance will be much higher.
Second version using static
Replace
method:
using System.Text.regularExpressions;
string filename = "B1_1-A.dwg";
filename = Regex.Replace(filename, @"_.*", "");
[Edit] My bad, I misread the question. Should not answer before my second coffee, it seems. I modified the solution. [/Edit]
[Edit2] Tested it; does not match. Still searching [/Edit2]
[Edit3] Now working. Weird that it matches with
*
but not
?
. [/Edit3]