Results 1 to 13 of 13
  1. #1
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: String Contains any one of (Word97/SR2)

    This should be efficient for relatively small numbers of matches:
    <pre>Function reFirstMatchPos(strExpression As String, strChars As String, _
    Optional blnCaseSensitive As Boolean = False) As Integer
    'Jefferson Scher 12/31/2002; Happy New Year!
    'returns first position of match of any character in strChars
    'requires VBScript 5 = IE 5.x
    reFirstMatchPos = 0
    'perform minimal parameter checking
    If (strExpression = vbNullString) Or (strChars = vbNullString) Then
    Exit Function
    End If
    'set up RegExp object
    Dim objRegExp As Object, colMatches As Object, objMatch As Object
    Set objRegExp = CreateObject("vbscript.regexp")
    With objRegExp
    .IgnoreCase = Not blnCaseSensitive
    .Pattern = "[" & strChars & "]"
    Set colMatches = .Execute(strExpression)
    End With
    If colMatches.Count = 0 Then
    Exit Function
    End If
    'preset the initial return value (zero based character position...)
    reFirstMatchPos = colMatches(0).FirstIndex + 1
    For Each objMatch In colMatches
    If objMatch.FirstIndex + 1 < reFirstMatchPos Then
    reFirstMatchPos = objMatch.FirstIndex + 1
    End If
    Next
    End Function

    Sub TestREMatch()
    Dim strPhrase As String, strInput As String
    strPhrase = "All your wishes granted by Microsoft!"
    strInput = InputBox("Characters to seek?")
    If Trim(strInput) = vbNullString Then Exit Sub
    Dim intPosition As Integer
    intPosition = reFirstMatchPos(strPhrase, strInput, False)
    If intPosition = 0 Then
    MsgBox "None of your characters - " & strInput & " - were found in the " & _
    "phrase '" & strPhrase & "'."
    Else
    MsgBox "One of your characters - the " & Mid(strPhrase, intPosition, 1) & _
    " from " & strInput & " - was the first to appear in the phrase '" & _
    strPhrase & "', at position " & CStr(intPosition) & "."
    End If
    End Sub</pre>

    Note: this will not match special characters unless you precede them with a backslash. More info on MSDN.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: String Contains any one of (Word97/SR2)

    > MsgBox lngStringContains("Christopher", "aeiou") ' 10 - position of "e" in "Christopher"

    Oops. Mine returns a match on the i, since that occurs first in Christopher. Accordingly, if you need to get 10, my code doesn't help.

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    String Contains any one of (Word97/SR2)

    <P ID="edit" class=small>(Edited by gwhitfield on 01-Jan-03 19:22. Comments moved to new lines- to avoid wide post)</P>I would appreciate any clues as to the execution-time efficient method of determining if one string contains any one of the characters in another string.


    For example, in a given chunk of text "strText" I'd like to know if it is worth my while examining the string in more detail.

    I'm not worried about the execution speed of what I do in the detail, but I am worried about the execution speed of determining if I should even START to examine it in detail.

    The sample function below (lngStringContains) strips characters one by one using RIGHT$ and LEN. I daresay looping with an integer might be marginally faster.

    I've tried using the LIKE operator, but I can't seem to make it match one complete string against another. I think I'd like a cousin of the LIKE operator, called ANYOF.




    <pre>Public Function lngStringContains(strText As String, ByVal strGoal As String) As Long
    ' If any one of the characters in strGoal can be found in the string strText,
    ' Then return the position of that one character within strtext.
    lngStringContains = 0 ' default result is failure-to-find
    Dim lngResult As Long
    While strGoal <> ""
    lngResult = InStr(1, strText, Left$(strGoal, 1))
    If lngResult > 0 Then ' success.
    lngStringContains = lngResult
    Exit Function
    Else
    strGoal = Right$(strGoal, Len(strGoal) - 1)
    End If
    Wend
    End Function
    Public Sub TESTlngStringContains()
    ' 10 - position of "e" in "Christopher"
    MsgBox lngStringContains("Christopher", "aeiou")
    ' 4 - position of "i" in "Christopher"
    MsgBox lngStringContains("Christopher", "iae")
    ' 0 - not one of "x", "y" and "z" appears in "Christopher"
    MsgBox lngStringContains("Christopher", "xyz")
    End Sub
    </pre>


  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: String Contains any one of (Word97/SR2)

    Thanks, jefferson, but that was *MY* Ooops - I had edited the text after pasting, trying to make a more sensible test, effectively making it worse.

    I am feeling particularly dense this week. I can't find the VBScript library.

    I have IE installed (and it loads), and I have followed other VBA forum threads by searching for "vbscript reference".

    I've found something called c:windowssystemVBScript.DLL and referenced it, but your code comes unglued at run-time at the line :

    <pre>reFirstMatchPos = colMatches(0).FirstIndex + 1
    </pre>


    with a diagnostic '438 Object doesn't support this property or method'.


    Perhaps I have the wrong DLL referenced.

    Could you please post the path/file name of the DLL you are using for vbscript?


    I also re-read some of your earlier posts. Given the amount of string-manipulation I do, I think I should take a serious look at regexp.

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: String Contains any one of (Word97/SR2)

    >registry have a Class definition for VBScript.RegExp

    Yes. There appear to be three. And yes, I have IE5 installed from my Win98 CD, I guess.


    > VBScript Regular Expressions

    ... but there is no sign of this string in my registry.


    Your code runs past the CreateObject line, but the created object is empty, so something is missing from my machine.

    Do you suppose that a straight re-install of IE5 from my Win98CD would fix things, or should i first delete all regirty references to vbScript.dll?

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: String Contains any one of (Word97/SR2)

    You shouldn't need a reference, I used late binding. Does your registry have a Class definition for VBScript.RegExp ? It installs with IE 5 or later.

    (But if you really want to know, the reference is called Microsoft VBScript Regular Expressions 1.0 - the 1.0 part might be new, there is now a 5.5 in my list!)
    Attached Images Attached Images

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: String Contains any one of (Word97/SR2)

    I exported what I think are the relevant bits from the registry. A reinstall might be most swift.

    (Also, I added a little picture to my previous post to economize on posts.)
    Attached Files Attached Files

  8. #8
    Star Lounger
    Join Date
    Aug 2001
    Location
    GA
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: String Contains any one of (Word97/SR2)

    Your question was : ....determining if one string contains any one of the characters in another string.
    If you just need to know if the token characters are in the string or not, this can be done with one line of code.

    Public Function DoesStringContainAnyOfTheseChar(strText As String, ByVal strGoal As String) As Boolean
    ' If any one of the characters in strGoal can be found in the string strText, then return TRUE else False.
    DoesStringContainAnyOfTheseChar = (strText Like "*[" & strGoal & "]*" )

    End Function

    Note that the behavior of the Like operator depends on the Option Compare setting. Unless you specify otherwise, each module uses Option compare Binary (case-sensitive comparisons).

    You could add another parameter to the function, blnIsCaseSensitive, along with an IF..end If to make it more flexible.

    As a side note:
    Your example of "Christopher" with "aeiou" only lets you know about the first find and the result will be different depending on the order of the characters passed in your strGoal variable. If the order of passed characters is changed, the answer could be 4,7, or 10.

    If you are searching for the character position to do something else with it, like replace the tokens with another character or remove them from the string then something else will be needed.

    Later,
    Bruce

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: String Contains any one of (Word97/SR2)

    Sheesh, Like works after all?! Chris, I want my holiday back. <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>

    This Option Compare issue is one I hadn't thought about before. I often force strings to UCase for comparison with the = operator. I will have to think about whether it is better to change my Option Compare default, to streamline the coding, or safer not to, for the day I forget or accidentally delete it. I guess if you are writing a custom function which uses Like, you could have an optional boolean for case insensitivity and use UCase to handle that "locally" in the function. Fun stuff. Thanks for posting.

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: String Contains any one of (Word97/SR2)

    Bruce, thanks, and yes, your function works.

    I had previously tried LIKE, remembering a thread of a couple of years back, but couldn't get it to work.

    Now it works, so I can QUICKLY test to see if further analysis is required.

  11. #11
    Star Lounger
    Join Date
    Aug 2001
    Location
    GA
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: String Contains any one of (Word97/SR2)

    If you liked that LIKE, check this out to see what else you may like about LIKE!
    Susan Harkins wrote an artical for WAW, it covered cool ways to use the Like operator.
    What's not to like about Like?

    IMHO, It has a coolness factor of about 85!

    Later,
    bruce

  12. #12
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: String Contains any one of (Word97/SR2)

    > If you liked that LIKE,

    Oh, I like LIKE. I was disappointed when, for this test, I couldn't seem to get it to work. Your method paves, as they say, the way.

    I do a lot of string manipulation, and testing patterns forms a large part of my work. After all, if you can't identify the problem, how can you place a solution.



    >It has a coolness factor of about 85!

    Well, like, I'd give it a higher factor. How high can it go? Would 132 be considered, like, out of order?


    Thanks for the link.

  13. #13
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: String Contains any one of (Word97/SR2)

    Jefferson, thanks for this. My delay in replying is a result of biting the bullet and re-installing Win98SE/Off97SR2. Well, actually, FDISKing and all else, switching from 10x2G partitions to one 20G.

    Still no sign of VBSCRIPT, and neither could i see it on my XP system.

    I've done something "wrong", and will go out on the web to look for more hints before continuing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •