|
-
Mar 21st, 2013, 02:25 PM
#1
Thread Starter
Lively Member
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
-
Mar 21st, 2013, 02:30 PM
#2
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)
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
-
Mar 21st, 2013, 02:31 PM
#3
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.
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Mar 21st, 2013, 02:32 PM
#4
Thread Starter
Lively Member
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)
-
Mar 21st, 2013, 02:41 PM
#5
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..
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Mar 21st, 2013, 02:41 PM
#6
Thread Starter
Lively Member
Re: Extracting Part of a String
Thanks alot! that should be no problem 
Thanks for the fast replies, im sure i'll reply with a problem in a few minutes
-
Mar 21st, 2013, 03:47 PM
#7
Thread Starter
Lively Member
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
-
Mar 22nd, 2013, 01:01 AM
#8
Re: Extracting Part of a String
 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!
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|