Here is the string I am looking to modify: 170-0175-00B-BEARING PLATE MACHINING.asm:2
I want to keep "170-0175-00B". So I need to remove the third hyphen and whatever is after it.
A rapid solution
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);
of course, if you are searching for the last hyphen then is more simple to do
int index = test.LastIndexOf('-');
if(index > 0)
test = test.Substring(0, index);
What about some LINQ?
Dim str As String = "170-0175-00B-BEARING PLATE MACHINING.asm:2"
MsgBox(String.Join("-"c, str.Split("-"c).Take(3)))
With this approach, you can take anything out after Nth hyphen, where N is easily controlled (a const).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With