locked
recovering numbers from string RRS feed

  • Question

  • I have a string coming from a file that when read is like this:

    "2773    " & vdTab & "4885   "  & vdTab &  "963"

    or like this:

    "2773    4885   963"

    i would like to get:

    a(0)=230

    a(1)=4885

    a(2)=963

    with:

     a = b.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)

    i resolve the second case, but not the first. Is there a general way to have only the numbers, even if the string is:

    "2773  not numbers  4885 not numbers  963" ?

    Thanks, Enzo

    Thursday, June 11, 2020 1:51 PM

Answers

  • Try an experimental approach too:

    Dim line = "1234 not numbers -1.87996E-1;5678"
    
    Dim numbers As New List(Of Double)
    
    For s = 0 To line.Length - 1
    
        For e = line.Length - 1 To s Step -1
    
            Dim n As Double
    
            If Double.TryParse(line.Substring(s, e - s + 1), n) Then
    
                numbers.Add(n)
    
                s = e
    
                Exit For
    
            End If
    
        Next
    
    Next

    Thursday, June 11, 2020 6:08 PM

All replies

  • A way =>

    Dim sString As String = "2773    " & vbTab & "4885   " & vbTab & "963"
    Dim sArrayNumbers() As String = System.Text.RegularExpressions.Regex.Split(sString, "\D+")
    For Each sNumber As String In sArrayNumbers
        Console.WriteLine("Number : {0}", Integer.Parse(sNumber))
    Next

    Thursday, June 11, 2020 2:14 PM
  • Try this

    Dim sampleData As String = "2773    " & vbTab & "4885   " & vbTab & "963"
    
    Dim values() As String = Regex.Split(sampleData, "\D+")
    For Each value As String In values
        If Not String.IsNullOrWhiteSpace(value) Then
            Dim result As Integer = Integer.Parse(value)
            Console.WriteLine($"Number: {result}")
        End If
    Next


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, June 11, 2020 2:17 PM
  • Thanks, but there are problems.

    I did not explain well what i wanted.

    A  number : -1.87996E-1  (that means -0.187996) becomes:

    1  87996 1


    Thursday, June 11, 2020 5:18 PM
  • Try an experimental approach too:

    Dim line = "1234 not numbers -1.87996E-1;5678"
    
    Dim numbers As New List(Of Double)
    
    For s = 0 To line.Length - 1
    
        For e = line.Length - 1 To s Step -1
    
            Dim n As Double
    
            If Double.TryParse(line.Substring(s, e - s + 1), n) Then
    
                numbers.Add(n)
    
                s = e
    
                Exit For
    
            End If
    
        Next
    
    Next

    Thursday, June 11, 2020 6:08 PM
  • It works.

    Thanks.

    Now I have to understand your lines.

    Enzo

    Thursday, June 11, 2020 7:10 PM
  • For the record Regex is regular expressions (read your deleted post), to use it you need the following using statement.

    using System.Text.RegularExpressions;


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, June 11, 2020 8:24 PM