0

My request is one that can extract a number somewhat by a search.

Example: animalsOwned|4 would return an containing "4"

animals|3|2|1|3 would return an array containing "3", "2", "1", "3"

This would make it easier for me during a file stream reader. Thank you

4 Answers 4

2
Dim astring = "ABCDE|1|2|3|4"

Dim numbers = (From s In astring
               Where Char.IsDigit(s)
               Select Int32.Parse(s)).ToArray()

This LINQ statement should help. It simply checks each character in a string to see if it's a digit. Note that this only applies to single digit numbers. It becomes a bit more complicated if you want "ABC123" to return 123 vs. 1, 2, 3 array.

Sign up to request clarification or add additional context in comments.

5 Comments

Right that's along the lines I'm looking for, but what if the string contains: "ABCDE|2 ABC|2|2|4" and I want to retrieve "2" by searching "ABCDE" ?
@Alex I think I understand what you are saying. It appears in your comment that you have a "space" between two strings (ABCDE|2 and ABC|2|2|4). In that case, you could simply split the string into two string arrays using the .Split method. Then use the approach I suggested above.
Without knowing exactly how your file is standardized, there could be all kinds of weird solutions. Regex may be in order if you have something very convoluted.
Thanks a lot for the help! With a little bit of tweeking the regex answer was correct!
You can use the .join method at the end of the array to return a full string if you need to.
1

Try regular expression. It's a powerful tool for simple text parsing.

Imports System.Text.RegularExpressions
Namespace Demo
    Class Program
        Shared Function Main(ByVal args As String()) As Integer
            Dim array As Integer() = ExtractIntegers("animals|3|2|1|3")
            For Each i In array
                Console.WriteLine(i)
            Next
            Return 0
        End Function
        Shared Function ExtractIntegers(ByVal input As String) As Integer()
            Dim pattern As String = "animals(\|(?<number>[0-9]+))*"
            Dim match As Match = Regex.Match(input, pattern)
            Dim list As New List(Of Integer)
            If match.Success Then
                For Each capture As Capture In match.Groups("number").Captures
                    list.Add(Integer.Parse(capture.Value))
                Next
            End If
            Return list.ToArray()
        End Function
    End Class
End Namespace

1 Comment

Thanks a lot! That's along the lines of what I wanted :) Thank you!
0

I haven't programmed VB for awhile but I'll give you some pseudo code: First, loop through each line of file. Call this variable Line. Then, take the index of what you're searching for: like Line.indexOf("animalsOwned") If it returns -1 it isn't there; continue. Once you find it, add the Index variable to the length of the search string and 1. (Index=Index+1+Len(searchString)) Then, take a substring starting there, and end at the end of the line. Explode the substring by | characters, then add each into an array. Return the array.

Sorry that I can't give you much help, but I'm working on an important PHP website right now ;).

Comments

0

You can do a variable.Split("|") and then assign each piece to an array level.

You can do a count on string and with a while or for loop, you can assign the splited sections to array levels. Then you can do a IsNumeric() check for each array level.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.