Solved

How to two compare a string

Posted on 2014-11-12
18
82 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 62

Expert Comment

by:Fernando Soto
Comment Utility
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 35

Expert Comment

by:Kimputer
Comment Utility
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
Comment Utility
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
 

Author Comment

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

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
OK?
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
What do you mean by OK?
0
 

Author Comment

by:cmdolcet
Comment Utility
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 62

Expert Comment

by:Fernando Soto
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:cmdolcet
Comment Utility
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 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
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 32

Expert Comment

by:it_saige
Comment Utility
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 62

Expert Comment

by:Fernando Soto
Comment Utility
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 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
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
Comment Utility
What is the "mask" assigned to?
0
 
LVL 85

Expert Comment

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

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

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

Expert Comment

by:Ark
Comment Utility
    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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now