Results 1 to 8 of 8

Thread: Extracting Part of a String

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2012
    Location
    London, England
    Posts
    119

    Question 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

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    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:
    1. Dim input As String = "543 Plastic Washer £0.39 12 £4.68"
    2.         Dim splitUp = input.Split(" ")
    3.         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.

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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!

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2012
    Location
    London, England
    Posts
    119

    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)

  5. #5
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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!

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2012
    Location
    London, England
    Posts
    119

    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2012
    Location
    London, England
    Posts
    119

    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

  8. #8
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Extracting Part of a String

    Quote Originally Posted by N00BPR0GRAMER View Post


    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
  •  



Click Here to Expand Forum to Full Width