Solved

How to two compare a string

Posted on 2014-11-12
18
85 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
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 35

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 62

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 62

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 62

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 62

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 33

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 62

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 85

Accepted Solution

by:
Mike Tomlinson earned 500 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 85

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 27

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

12 Experts available now in Live!

Get 1:1 Help Now