SubString Indexing issue

My substring value is “119270-2-1~5|10”
val = 119270-2-1~5|10

I can get the fist string of “119270-2-1” using

txtfield1.Text = val.Substring(0, val.IndexOf('~')); 

I can find the last string “10” using

numfield3.Value = Convert.ToInt32(val.Substring(val.LastIndexOf('|')+1));

However, I can’t get the middle value between the “~” and the “|”.
What I’ve tried so far

numfield2.Value = val.Substring(val.IndexOf('~'), val.IndexOf('|')); 
numfield2.Value = Convert.ToInt32(val.Substring(val.IndexOf('~'), val.IndexOf('|'));

This is the error I keep getting:
image

I’d imagine some of you could solve this much quicker than I can.

This is taken from code to split up string coming from a barcode, but it should work for what you want.

            string barcode = barcodeObj.ToString();
            // Split barcode into parts
            var items = barcode.Split('-');
            string job = items[1];
            string asm = items[2];
            string opr = items[3];

so split it twice, the first time with one delimiter, and the second time with the second.

3 Likes

Works great to split the string, just had to make a minor change on the sending form.

I think I should have it from here. Appreciate it, again!

1 Like

Oh, and I also had to change it from

            string job = items[1];
            string asm = items[2];
            string opr = items[3];

to

            string job = items[0];
            string asm = items[1];
            string opr = items[2];

but works great.