Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to two compare a string

Posted on 2014-11-12
18
Medium Priority
?
93 Views
Last Modified: 2014-11-17
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 "*"
0
Comment
Question by:cmdolcet
  • 7
  • 6
  • 2
  • +3
18 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40438262
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
 
LVL 37

Expert Comment

by:Kimputer
ID: 40438265
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
 

Author Comment

by:cmdolcet
ID: 40438280
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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 

Author Comment

by:cmdolcet
ID: 40438285
I would need to know where the wildacards are in reference to the actual numbers (123) I am looking for
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40438299
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
 

Author Comment

by:cmdolcet
ID: 40438304
OK?
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40438310
What do you mean by OK?
0
 

Author Comment

by:cmdolcet
ID: 40438312
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40438332
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
 

Author Comment

by:cmdolcet
ID: 40438341
The "*" can be any alphanumeric character. the "*" is only denoting that that in the string we do not care what those values are.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40438368
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
 

Author Comment

by:cmdolcet
ID: 40438380
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
 
LVL 35

Expert Comment

by:it_saige
ID: 40438420
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40438567
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 40438597
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
 

Author Comment

by:cmdolcet
ID: 40440239
What is the "mask" assigned to?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 40440285
It's the last parameter passed in to MatchStringsWithMask():

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

In the example, I used "*".
0
 
LVL 28

Expert Comment

by:Ark
ID: 40445305
    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

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question