Solved

comparing matchcase strings

Posted on 2004-09-25
18
187 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

932 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

19 Experts available now in Live!

Get 1:1 Help Now