Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

comparing matchcase strings

Posted on 2004-09-25
18
Medium Priority
?
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 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