Solved

comparing matchcase strings

Posted on 2004-09-25
18
190 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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
 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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