Question

I have a string which can have values like

"Barcode : X 2 4688000000000"

"Barcode : X 10 1234567890123"

etc.

I want to retrieve the quantity value (i.e., 2, 10) and the barcode value (i.e., 4688000000000, 1234567890123)

I tried the following code -

string[] QtyandBarcode = Regex.Split(NPDVariableMap.NPDUIBarcode.DisplayText, @"\D+");

But when I execute, I'm getting QtyandBarcode as a string array having 3 values -

""
"2"
"4688000000000"

How do I prevent the null value from being stored?

Était-ce utile?

La solution 2

string[] QtyandBarcode = Regex.Split(NPDVariableMap.NPDUIBarcode.DisplayText, @"\D+").Where(s => !string.IsNullOrEmpty(s)).ToArray();

now you can

string qty = QtyandBarcode[0];
string barcode= QtyandBarcode[1];

Autres conseils

You could do this:

MatchCollection m = Regex.Matches(NPDVariableMap.NPDUIBarcode.DisplayText, @"\d+");

var x = (from Match a in m select a.Value).ToArray();

What about this simple approach.

string[] parts =  NPDVariableMap.NPDUIBarcode.DisplayText.split(' ');  //split on space
string qty = parts[1];
string barcode = parts[2];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top