I have a dropdownlist that has the value of two columns in it… One column is a number ranging from 5 characters long to 8 characters long then a space then the ‘|’ character and another space, followed by a Description for the set of numbers.
An example:
12345678 | Description of Product
In order to pull the items for the dropdownlist into my database I need a to utilize a substring to pull the sequence of numbers out only.
Is it possible to write a substring to pull multiple character lengths? (Sometimes it may be 6 numbers, sometimes 5 numbers, sometimes 8, it would depend on what the user selected from the dropdownlist.)
Use a regular expression for this.
Assuming the number is at the start of the string, you can use the following:
Usage:
You could also use
string.Splitto get the parts separated by|if you know the first part is what you need and will always be numeric:Either of these approaches will result in a string. You can use
int.Parseon the result in order to get an integer from it.