解决方法
快速解决方案
string test = "170-0175-00B-BEARING PLATE MACHINING.asm:2"; int num = 2; int index = test.IndexOf('-'); while(index > 0 && num > 0) { index = test.IndexOf('-',index+1); num--; } if(index > 0) test = test.Substring(0,index);
当然,如果你要搜索最后一个连字符,那么更简单
int index = test.LastIndexOf('-'); if(index > 0) test = test.Substring(0,index);