How to two compare a string

I am trying to compare two strings one that has "****123***" and the other is the same string without the "*" so that string would be "1111123111"

Can anyone suggest a way to compare just valid AlphaNumeric numbers in a string not represented by "*"
cmdolcetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Are the characters "*" in this string "****123***" always "*"? Are the characters "1" in this string "1111123111" always "1"?  If they are not how do you determine which characters you want to search from string 1 in string 2.
0
KimputerCommented:
This would be my code, however, it disregards where the wildcards are as well as how many there are. Do you need that exactly? If so I can adjust the code.
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim wildcard_string = "****123***"
        Dim number_string = "1111123111"
        Dim match = InStr(number_string, Replace(wildcard_string, "*", ""))
        If match Then
            MsgBox("wildcard match found")
        Else
            MsgBox("wildcard match not found")
        End If
    End Sub

Open in new window

0
cmdolcetAuthor Commented:
Yes I want to use the "*" as a wildcard character so when I compare I really dont have to worry about the alphanumeric numbers that represent the "*" I am only concerned of the "123" for example and the position in the string. the comparing strng of "1" is only an example so those numbers would change.
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

cmdolcetAuthor Commented:
I would need to know where the wildacards are in reference to the actual numbers (123) I am looking for
0
Fernando SotoRetiredCommented:
Is the string "****123***" really those characters or could it be say the following string "13AC123ZX2"? If they are actually this "****123***" then fine.
0
cmdolcetAuthor Commented:
OK?
0
Fernando SotoRetiredCommented:
What do you mean by OK?
0
cmdolcetAuthor Commented:
How would I compare the two strings. Remember I also need to compare the position of the actual numbers I am looking for in the string.
0
Fernando SotoRetiredCommented:
Please answer this question..

Is the string "****123***" really those characters or could it be say the following string "13AC123ZX2"? If they are actually this "****123***" then fine. In other words are all the "*" in that string always "*" or can they be any alpha numeric characters?
0
cmdolcetAuthor Commented:
The "*" can be any alphanumeric character. the "*" is only denoting that that in the string we do not care what those values are.
0
Fernando SotoRetiredCommented:
Fine. Now the next question which I posted earlier as well. Let say that the first string, the one with all the "*", how do we know which characters in this string you wish to find in string two, the one with all the "1"'s in it?
0
cmdolcetAuthor Commented:
OK, what I was thinking is going character by character comparing the position and whether the value matched.

For example:

"****123**" I would see if the second string if position 5 equaled "1" position 5 equaled "2" and position 6 equaled "3"
0
it_saigeDeveloperCommented:
Without regard to duplicates of the wanted value and the length of the strings, you could simply do this:
	Public Shared Function ContainsValue(ByVal data1 As String, ByVal data2 As String, ByVal value As String) As Boolean
		Return data2.Contains(value) AndAlso data1.Contains(value) AndAlso data2.IndexOf(value) = data1.IndexOf(value)
	End Function

Open in new window


And use it like:
Shared Sub Main()
	Try
		Dim test1 As String = "*****123*****"
		Dim test2 As String = "CDEX1123YUN1X"
		Console.WriteLine(String.Format("String1 and String2 contain the same value: {0}", ContainsValue(test1, test2, "123")))
		test1 = "*****123*****"
		test2 = "CDEX1456YUN1XY"
		Console.WriteLine(String.Format("String1 and String2 contain the same value: {0}", ContainsValue(test1, test2, "123")))
	Catch e As Exception
		Console.WriteLine(e.ToString())
	End Try
	Console.ReadLine()
End Sub

Open in new window


This then could be converted to an extension method:
Public Module Extensions
	<System.Runtime.CompilerServices.Extension()> _
	Public Function ContainsValue(ByVal data1 As String, ByVal data2 As String, ByVal value As String) As Boolean
		Return data2.Contains(value) AndAlso data1.Contains(value) AndAlso data2.IndexOf(value) = data1.IndexOf(value)
	End Function
End Module

Open in new window


And called like this:
Shared Sub Main()
	Try
		Dim test1 As String = "*****123*****"
		Dim test2 As String = "CDEX1123YUN1X"
		Console.WriteLine(String.Format("String1 and String2 contain the same value: {0}", test1.ContainsValue(test2, "123")))
		test1 = "*****123*****"
		test2 = "CDEX1456YUN1XY"
		Console.WriteLine(String.Format("String1 and String2 contain the same value: {0}", test1.ContainsValue(test2, "123")))
	Catch e As Exception
		Console.WriteLine(e.ToString())
	End Try
	Console.ReadLine()
End Sub

Open in new window


Either way the results are the same:Capture.JPG
-saige-
0
Fernando SotoRetiredCommented:
Hi cmdolcet;

The following code sample will locate consecutive characters from string1 in string2. It will identify the location in string2 and the length.

'' Sample data
Dim string1 As String = "****123*11"
Dim string2 As String = "1111123111"
Dim searchLen As Integer = 0

'' Return the shortest string of the sample data
If string1.Length = string2.Length Then
    searchLen = string1.Length
ElseIf string1.Length < string2.Length Then
    searchLen = string1.Length
Else
    searchLen = string2.Length
End If

'' Maintains the index and of the characters in the word found
Dim seqChars As New List(Of List(Of Integer))

'' Find the chars in string1 in string2 at the same character position that is more then two characters long
For i As Integer = 0 To searchLen - 1
    If string1(i) = string2(i) Then
        '' Maintains words between previous finds
        Dim wordsFound As New List(Of Integer)
        wordsFound.Add(i)
        '' see if the next characters come directly after the one just found.
        For j As Integer = i + 1 To searchLen - 1
            If string1(j) = string2(j) Then
                wordsFound.Add(j)
            Else
                i = j
                '' Last character does not match end of word
                Exit For
            End If
        Next
        '' If found more then one characters we found a word add it to the list
        If wordsFound.Count > 1 Then
            seqChars.Add(wordsFound)
        End If
    End If
Next

'' Report the start index and length of the words found
For Each l1 As List(Of Integer) In seqChars
    Console.WriteLine("Word starts at index {0} with a length of {1}", l1(0), l1.Count())
Next

Open in new window

0
Mike TomlinsonMiddle School Assistant TeacherCommented:
I ~think~ this is what you're trying to do?...
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim strA As String = "****123***"
        Dim strB As String = "1111123111"
        Dim match As Boolean = MatchStringsWithMask(strA, strB, "*")
        MessageBox.Show("match = " & match)
    End Sub

    Public Function MatchStringsWithMask(ByVal StringWithMask As String, ByVal StringWithoutMask As String, ByVal Mask As String) As Boolean
        If StringWithMask.Length = StringWithoutMask.Length Then
            For i As Integer = 0 To StringWithMask.Length - 1
                If StringWithMask.Substring(i, 1) <> Mask Then
                    If StringWithMask.Substring(i, 1) <> StringWithoutMask.Substring(i, 1) Then
                        Return False
                    End If
                End If
            Next
            Return True
        Else
            Return False
        End If
    End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cmdolcetAuthor Commented:
What is the "mask" assigned to?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
It's the last parameter passed in to MatchStringsWithMask():

    Dim match As Boolean = MatchStringsWithMask(strA, strB, "*")

In the example, I used "*".
0
ArkCommented:
    Private Function StringMatch(ByVal source As String, ByVal pattern As String) As Boolean
        pattern = pattern.Replace("*"c, "?"c)
        Return (source Like pattern)
    End Function

Open in new window

Debug.Print(StringMatch("1111123111", "****123***")) 'True
Debug.Print(StringMatch("111112311", "****123***")) 'False
Debug.Print(StringMatch("A111123111", "****123***")) 'True
Debug.Print(StringMatch("ABCD123EFG", "****123***")) 'True
Debug.Print(StringMatch("111123111", "****123***")) 'False

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.