Extracting Part of a String
Hey, basically I have a string that contains information about a product, it is all stored as one long string.
in order of appearance, it contains....
Product ID
Product Description
Product Cost
Quantity
Total Cost
I want to extract the quantity from the string
*String Example*
"543 Plastic Washer £0.39 12 £4.68"
Any help is appreciated
thanks
Re: Extracting Part of a String
Basically you'll want to split up the string on each space. Since "Product Description" will be broken up into multiple fields you can't say Quantity will be the 4th field so you'll need to get the next to last field in the array.
vb.net Code:
Dim input As String = "543 Plastic Washer £0.39 12 £4.68"
Dim splitUp = input.Split(" ")
Dim secondFromLast = splitUp(splitUp.Length - 2)
Re: Extracting Part of a String
For a start, look into the Split-Function. In your example I'd use the space as the delimeter that will determine whrer to split the string.
You will get an array filled with strings.
Re: Extracting Part of a String
Sounds good, do i then just read in the quantity as I would from a normal one dimensional array? (fairrrly new to this :S)
Re: Extracting Part of a String
Using it that way, you will read in a string, you need to convert that to a number like Integer or Double etc..
Re: Extracting Part of a String
Thanks alot! that should be no problem :D
Thanks for the fast replies, im sure i'll reply with a problem in a few minutes :p
Re: Extracting Part of a String
Okay so im confused as to how to use this?
I have a one dimensional array which contains all the strings about the products in it and I want to add the quantity to a list box
would it be something like..
Code:
Dim Splitup as string
Dim Secondfromlast as string
For index = 0 To PartsArray.GetUpperBound(0)
Dim splitUp = qtyfind.Split(" ")
Dim secondFromLast = splitUp(splitUp.Length - 2)
Quantity.Items.Add(secondFromLast)
Next
Re: Extracting Part of a String
Quote:
Originally Posted by
N00BPR0GRAMER
Code:
Dim Splitup as string
Dim Secondfromlast as string
For index = 0 To PartsArray.GetUpperBound(0) 'so your Array Containing all Data is called PartsArray
Dim splitUp = qtyfind.Split(" ") 'so you should use "PartsArray(index)" in here instead of "qtyfind"
Dim secondFromLast = splitUp(splitUp.Length - 2)
Quantity.Items.Add(secondFromLast)
Next
..added two comments to your code!