Solved

comparing matchcase strings

Posted on 2004-09-25
18
186 Views
Last Modified: 2010-04-23
i have string one and i have string two. I want to compare the 2 strings. If string one has * in it ,then it will be considered to be a string like below

eg ca*   = car,care, cat   etc

how can this be done?
0
Comment
Question by:abababa
  • 4
  • 3
  • 3
  • +3
18 Comments
 
LVL 18

Expert Comment

by:armoghan
ID: 12149994

Why dont you use StartsWith function ?
Dim sms1 As String = "care"

If sms2.StartsWith("car") = True Then
Console.WriteLine("yes")
        End If
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12149997
and use substring to get the ca before *
Dim sms2 As String = "ca*"
sms2.Substring (0, sms2.IndexOf ("*"))

This will return "ca"
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12150002
Dim sms1 As String = "ca*"
Dim sms2 As String = "care"

If sms2.StartsWith(sms1.Substring (0, sms1.IndexOf ("*"))) = True Then
Console.WriteLine("yes")
End If
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12150337
       Dim str As String = "ca*"
        Dim str2 As String = "care"

        'If the last char is an * then compare the first chars        
        If Right(str, 1) = "*" Then
            'If the string matched was found starting from the first char return true otherwise false
            Debug.WriteLine(IIf(InStr(str2, Left(str, 1)) = 1, True, False))
        Else
            Debug.WriteLine(str = str2)
        End If
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12150805
test code below
and you can found the index of first index of match string

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim s As String = "ca*"
    Dim searchs As String = "wcare"

    Dim reg As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("ca*")
    Dim m As System.Text.RegularExpressions.Match = reg.Match(searchs)
    If (m.Success) Then
        MessageBox.Show(m.Index.ToString())
    End If
End Sub
0
 

Author Comment

by:abababa
ID: 12152353
sorry but i dont know if any of those do what word does

if someone puts c*r*

this could mean carmen,care,curry  , i dont think any of the above answers do this
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12152448
I test the input you write,its ok
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Expert Comment

by:webwebwebweb
ID: 12153981
 so i used ur code and got  this.  I change the searchs string to carmen,care and it brings up a message box saying its matched but wen i put in cd it still brings up that it matches but it should be that it doesnt match, what is not right here?


 Dim s As String = "ca*"
 Dim searchs As String = "wcare"

 Dim reg As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("ca*")
        Dim m As System.Text.RegularExpressions.Match = reg.Match(searchs)
        If (m.Success) Then
            MsgBox("match")
        Else
            MsgBox("no match")
        End If
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12153993
sorry for a little mistake

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim s As String = "ca*"
    Dim searchs As String = "wcare"

    Dim reg As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(s)'<-this should be s not "ca*" as we want to change the search pattern
    Dim m As System.Text.RegularExpressions.Match = reg.Match(searchs)
    If (m.Success) Then
        MessageBox.Show("I found at "+m.Index.ToString())
    else
        MessageBox.Show("sorry not found")
    End If
End Sub
0
 

Expert Comment

by:webwebwebweb
ID: 12154012
i put in the following and it brings back that it matches, c shouldnt match but it does and i tried putting d in searchs and it says no match so thats working fine. It seems if its got a c then it matches

Dim s As String = "ca*"
Dim searchs As String = "c"

Dim reg As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(s) '<-this should be s not "ca*" as we want to change the search pattern
        Dim m As System.Text.RegularExpressions.Match = reg.Match(searchs)
        If (m.Success) Then
            MsgBox("match")
        Else
            MsgBox("no match")
        End If
0
 

Author Comment

by:abababa
ID: 12154024
yeh thats what happens
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12159957
for maching such strings c*r*
You need to use Regular Expressions..
For simple things like c* you can use the code i mentioend above
0
 
LVL 4

Accepted Solution

by:
eozz_2000 earned 300 total points
ID: 12167033
I did this function, try with it:

    Private Function Matches(ByVal strPattern As String, ByVal strString As String) As Boolean
        If strPattern.IndexOf("*") = 0 Then
            If strPattern.Length = 1 Then
                Return True
            Else
                If strPattern.Substring(1, strPattern.Length - 1).IndexOf("*") <> -1 Then
                    Dim strBetweenAsterisks As String
                    strBetweenAsterisks = strPattern.Substring(1, strPattern.Substring(1, strPattern.Length - 1).IndexOf("*"))
                    If strString.IndexOf(strBetweenAsterisks) <> -1 Then
                        Return Me.Matches(strPattern.Substring(1, strPattern.Length - 1), strString.Substring(strString.IndexOf(strBetweenAsterisks), strString.Length - strString.IndexOf(strBetweenAsterisks)))
                    Else
                        Return False
                    End If
                Else
                    Dim strAfterAsterisk As String
                    strAfterAsterisk = strPattern.Substring(1, strPattern.Length - 1)
                    If strString.IndexOf(strAfterAsterisk) <> -1 And strString.IndexOf(strAfterAsterisk) + strAfterAsterisk.Length = strString.Length Then
                        Return True
                    Else
                        Return False
                    End If
                End If
            End If
        Else
            If strPattern.Substring(1, strPattern.Length - 1).IndexOf("*") <> -1 Then
                If strString.StartsWith(strPattern.Substring(0, strPattern.IndexOf("*"))) Then
                    Dim strFromAsterisk As String
                    strFromAsterisk = strPattern.Substring(strPattern.IndexOf("*"), strPattern.Length - strPattern.IndexOf("*"))
                    Return Me.Matches(strFromAsterisk, strString.Substring(strPattern.Substring(0, strPattern.IndexOf("*")).Length, strString.Length - strPattern.Substring(0, strPattern.IndexOf("*")).Length))
                Else
                    Return False
                End If
            Else
                If strPattern = strString Then
                    Return True
                Else
                    Return False
                End If
            End If
        End If
    End Function

You have only to validate the strPattern <> ""
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12245292
Hey man, I have already fixed your problem with this function... WHERE ARE YOU?
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12453491
Well... I have solved the problem... where are my points?, is it valid to ask and leave the question that has already an answer?
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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…

758 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

17 Experts available now in Live!

Get 1:1 Help Now