Results 1 to 5 of 5

Thread: [RESOLVED] Removing non numeric chars from a string

  1. #1

    Thread Starter
    Junior Member JaGrom's Avatar
    Join Date
    Nov 2006
    Location
    Rome (Italy)
    Posts
    17

    Resolved [RESOLVED] Removing non numeric chars from a string

    Hello Everybody!

    I need help with this task:

    I have a flexgrid column filled with data that contains various chars but I want to eliminate all non-numerical and leave only the numbers.

    Can anyone tell me how to write the code?

    Thanks

  2. #2
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Removing non numeric chars from a string

    Are you talking about discrete numerals (like 1 or 4 or 8 etc) or are they actual numbers like 345 4.32 1,000.45 etc?
    VB 2005, Win Xp Pro sp2

  3. #3

    Thread Starter
    Junior Member JaGrom's Avatar
    Join Date
    Nov 2006
    Location
    Rome (Italy)
    Posts
    17

    Re: Removing non numeric chars from a string

    Actual numbers. Many of them are decimals

  4. #4
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Removing non numeric chars from a string

    Code:
            Dim myString As String = "aaa34BB12,000xcv9.9zz"
            Dim ResultString As String
    
            Dim i As Integer
    
            For i = 0 To myString.Length - 1
                If IsNumeric(myString.Chars(i)) = True OrElse myString.Chars(i) = "." OrElse myString.Chars(i) = "," Then
                    ResultString &= myString.Chars(i)
                End If
            Next
    
            MessageBox.Show(ResultString)
    It will create another string with only numbers, commas and periods. If your string has at least one empty space between the numbers, you can also check if the current char is an empty space and add it to the new string too.

    Also, if the original string contains lots of numbers, it will be faster if you use StringBuilder instead of another string.
    VB 2005, Win Xp Pro sp2

  5. #5

    Thread Starter
    Junior Member JaGrom's Avatar
    Join Date
    Nov 2006
    Location
    Rome (Italy)
    Posts
    17

    Re: Removing non numeric chars from a string

    It works! Thanks a lot!

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