Hey Pals,

I was trying very hard to compare 2 numbers when one is not in any sequence.
EG: First number: 1233
Second Number: 3321
In this case above, i want it to match because they both contains same numbers, just that it is not in sequence.

I have tried this code but it doesn't seem to work.

              Dim n1 = "1233"
                If n1.Contains("1332") Then
                'Do Something
                End if

Thanks a million times!

That's because Contains checks for an exact occurrence of the substring. I'm not very familiar with VB.NET so I'm sure there are better solutions out there than the ones I'm posting, especially length and time wise. But the problem itself could be solved in several ways (not a complete list nor in any particular order):

  • Sort both numerical strings, then compare
  • Count the numbers 0-9 in n1 and again for n2 and see if the amounts match

For non numbers as well:

  • Split both and iterate over all characters, check if a character from n1 occurs in n2, if it does remove from both arrays, if you have residual characters in either the strings do not match
  • Similarly you could take n1, iterate over each character and remove a single occurrence of that character from n2, if n2 has characters left it is not a match

By checking for edge cases beforehand you can shorten the running time of whichever method you decide to pick. Check for null and empty strings. If the strings are not the same length they can't match. If you do numeric operations have it return on a conversion error (i.e. String is not a number).

You can use these 30 String operations as a reference on how to do some of the above.

Similar reference for sorting in VB.NET.

commented: Nice! +15

Contains() always returns boolean value. You an use it to match between two strings or to get an bolean value if a substring exists in a string or not.
If it exists or match it returns True else False.
You can not use it to compare between two numbers.
@coolsasuke:
From your example : "1233" & "1332" are not same by numerically or if you treat them as string, they are also not same, though by combination they both are build by "1","2" & "3". So it always returns False.

It is not very clear what you are asking. In your definition 2 numbers are equal if they contain the same digits. So |123| = |312| etc. But what about |123| =? |11332222|? If they contain the same numbers of digits then you can pu them into arrays and sort them: 123 = 123 and then just see if str1=str2. Your approach also leads to a result but you need to compare each character of the second string(Loop):

If n1.Contains("1") Then
                'Do Something
                End if

Hello everyone, thanks for your reply.
Initially i did thought of rearranging the numbers into ascending order. But then i actually wanted something different.
Lets say number = 1353.
At first, i was trying to figure out on how to detect the same double numbers; A code that will detect that the '3' in this 4 digit numbers has occured two times. And for 3133, it will detect that it has occurred three times.
As well for 5533, it will detect it as 2 pairs of same numbers.
And for 1234, it will detect them as a whole different numbers.
Anyone would be able to help me with that?

Thanks!

Taking a two dimensional array you can do it.
The codes should be like

        'Declaring variables
        Dim numstr(1, 0) As String
        Dim arel As Integer = 0
        Dim count As Integer = 0


        'Try to redimention the array and store the charecters or numbers in array
        ReDim numstr(1, arel)
        For i As Integer = 1 To TextBox1.Text.Length

            Dim match As Boolean = False


            For j As Integer = 0 To numstr.GetLength(1) - 1
                If numstr(0, j) = Mid(TextBox1.Text, i, 1) Then
                    match = True
                    Exit For
                End If
            Next

            If match = False Then
                ReDim Preserve numstr(1, arel)
                numstr(0, arel) = Mid(TextBox1.Text, i, 1)
                numstr(1, arel) = "1"
                arel += 1
            End If

        Next


        'Count the number of occurance for each element value
        'in the array and store it in the respective position
        For i As Integer = 0 To numstr.GetLength(1) - 1
            For j As Integer = 1 To TextBox1.Text.Length
                If Mid(TextBox1.Text, j, 1) = numstr(0, i) Then
                    count += 1
                End If
            Next
            numstr(1, i) = count.ToString()
            count = 0
        Next


        'Now sort the array
        Dim xstr As String = ""
        For i As Integer = 0 To numstr.GetLength(1) - 2
            For j As Integer = 0 To numstr.GetLength(1) - 2
                If numstr(0, j) > numstr(0, j + 1) Then
                    xstr = numstr(0, j)
                    numstr(0, j) = numstr(0, j + 1)
                    numstr(0, j + 1) = xstr

                    xstr = numstr(1, j)
                    numstr(1, j) = numstr(1, j + 1)
                    numstr(1, j + 1) = xstr

                End If
            Next
        Next


        ' now show them
        Dim x As String = "Charector - Occurance" & vbCrLf
        For i As Integer = 0 To numstr.GetLength(1) - 1
            x &= Space(10) & numstr(0, i) & Space(20) & numstr(1, i) & vbCrLf
        Next
        MessageBox.Show(x)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.