How to get integers from a string?

Hi iam a new guy for VB

My question is how to get the integer numbers from a string,(For example: NewText="Asdjskjd 303 6845"and i only want 303 6845)

Thank you




Answer this question

How to get integers from a string?

  • Vaish

    one way would be to go through each character, check if its a number, if so, add it to a collection or something.

    Dim theNumbersOnly as new System.Text.StringBuilder()

    for each currentChar as Char in theString

       if Char.IsNumber(currentChar) = true then

          theNumbersOnly.Append(currentChar)

       end if  

    next

     

    theString being the string that contains the letters/numbers



  • Aditya.P

    Thanks , what if the spaces between the numbers are necessary can i do somthing like to find the first

    number in the string and then get the next 8 numbers(including spaces) and How it can be done

    Thank you



  • MLyons10

    if the space is neccessary then you can alter the solution a bit to include the space:

    for each currentChar as Char in theString

    if Char.IsNumber(currentChar) = true Or Char.IsWhiteSpace(currentChar) then

    theNumbersOnly.Append(currentChar)

    end if

    next

    or you could:

    Or currentChar.Equals(' ')

    instead of "Or Char.IsWhiteSpace(currentChar)"



  • How to get integers from a string?