Results 1 to 26 of 26

Thread: £ $ % InStr Function RESOLVED

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Resolved £ $ % InStr Function RESOLVED

    Im trying to use the InStr function with the above.

    Code:
    If Instr(ID, "£") Then Call 
    
    If Instr(ID, """) Then Call
    doesn't like my code. Im trying to c if ID contains of the below symbols. It errored when i used the pound sign.

    " £ $ % ^ & * ( ) [ } [ { : ; ' , # ~ < > ? / | \
    Last edited by Ricky1; Oct 19th, 2005 at 11:44 AM.
    Im Learning !!!!

  2. #2
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667

    Re: £ $ % InStr Function

    Try the following you did not put a start pos in the instr function.

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim ID As String
    3.     ID = "Bomb£Drop"
    4.    
    5.     If InStr(1, ID, "£") Then
    6.         Debug.Print "Yep it's there"
    7.     Else
    8.         Debug.Print "sorry not there"
    9.     End If
    10.  
    11. End Sub

    Hope this helps!!

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: £ $ % InStr Function

    This will not only tell you if it is there, but how many times it appears in the string.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Function FindChar(ByVal pstrText As String, pintChar As Integer) As Long
    4.     Dim btyTextArray() As Byte
    5.     Dim i As Long
    6.     Dim lngCount As Long
    7.    
    8.     btyTextArray() = pstrText
    9.     For i = 0 To UBound(btyTextArray) Step 2
    10.         If btyTextArray(i) = pintChar Then lngCount = lngCount + 1
    11.     Next
    12.     FindChar = lngCount
    13.     MsgBox lngCount
    14. End Function
    15.  
    16. Private Sub Command1_Click()
    17. Dim lngFind As Long
    18. '163 is the ascii for £ sign
    19. lngFind = FindChar(Text1.Text, 163)
    20. End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: £ $ % InStr Function

    Ok seems to work but how would i check to see if ID contained " symbol.

    it doesn't like """
    Im Learning !!!!

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: £ $ % InStr Function

    so it would make it alot easier if i foundout the ACSII for all the symbols. Then i would be able to use the normal InStr function ?
    Im Learning !!!!

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: £ $ % InStr Function

    VB Code:
    1. 'In my post change
    2. lngFind = FindChar(Text1.Text, 163)
    3. 'to
    4. lngFind = FindChar(Text1.Text, 34)

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: £ $ % InStr Function

    Quote Originally Posted by Ricky1
    so it would make it alot easier if i foundout the ACSII for all the symbols. Then i would be able to use the normal InStr function ?
    It is very easy to accomplish this.

    In the immediate window type:

    ?asc("?")

    and hit enter. It will give you the ascii code for a question mark. Use that for any symbol for which you need the ascii code equivalent.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: £ $ % InStr Function

    In the immediate window type: ??
    Im Learning !!!!

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: £ $ % InStr Function

    Quote Originally Posted by Ricky1
    In the immediate window type: ??
    Yes...it is sometimes referred to as the "debug" window.

    When you place Debug.Print Something in your code, the Immediate Window is where is shows up.

    Do you know what I mean?

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: £ $ % InStr Function

    oh yeah i found it. I never have that window showing, Didn't know what it did.
    Thanks.
    Im Learning !!!!

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: £ $ % InStr Function RESOLVED

    sorry hack having problems with your code.

    Could you do it so if it finds the Symbol i can add to If decision so if it exists DOTHIS, if not DOTHIS. And without the count bit, i don't need that.
    Im Learning !!!!

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: £ $ % InStr Function

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim lngFind As Long
    3. lngFind = FindChar(Text1.Text, 163)
    4. If lngFind > 0 Then
    5.    'the character is there at least once
    6.    MsgBox "Yes sir.  That sucker is in that string."
    7. Else
    8.   'it is not found
    9.   MsgBox "Nope.   I looked all over the place, and cant find it anywhere."
    10. End If
    11. End Sub

  13. #13

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: £ $ % InStr Function

    i leave the original findchar function as it is?
    Im Learning !!!!

  15. #15
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: £ $ % InStr Function

    Quote Originally Posted by Ricky1
    i leave the original findchar function as it is?
    I didn't bother to change it, but you can do an Exit Function as soon as lngCount = 1 if you would prefer.

  16. #16
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: £ $ % InStr Function

    Ok seems to work but how would i check to see if ID contained " symbol.

    it doesn't like """
    You need to code it like this:
    VB Code:
    1. Instr(ID,"""")

    so it would make it alot easier if i foundout the ACSII for all the symbols. Then i would be able to use the normal InStr function ?
    Run this code:
    VB Code:
    1. For i = 32 to 255
    2.   Debug.Print i,Chr$(i)
    3. Next
    This world is not my home. I'm just passing through.

  17. #17
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: £ $ % InStr Function

    Hack, it would be more efficient to pass the string ByRef and the character as a Byte

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: £ $ % InStr Function

    wow im really confussed now. Can any1 post some code that will do the above for me. My first post.

    Im going down a list of words and if it contains any of the below symbols

    " £ $ % ^ & * ( ) [ } [ { : ; ' , # ~ < > ? / | \

    remove that item from the original list and add it to another list.
    Im Learning !!!!

  19. #19
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: £ $ % InStr Function

    VB Code:
    1. Property Get hasIllegalChars(ByRef pszSearch As String) As Boolean
    2. Static chIllegals() As Byte, fRun As Boolean
    3. Dim chBuf() As Byte, i As Long, j As Long
    4.  
    5.     If (Not fRun) Then
    6.         chIllegals = [color=dimgray]"""£$%^&*()[}[{:;'[/color][color=dimgray],#~<>?/|\"[/color]
    7.         fRun = True
    8.     End If
    9.  
    10.     chBuf = pszSearch
    11.  
    12.     For i = 0 To UBound(chBuf) Step 2
    13.         For j = 0 To UBound(chIllegals)
    14.             If (chBuf(i) = chIllegals(j)) Then
    15.                 hasIllegalChars = True
    16.                 Exit Property
    17.             End If
    18.         Next j
    19.     Next i
    20. End Property
    Last edited by penagate; Oct 19th, 2005 at 09:15 AM.

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: £ $ % InStr Function

    wow pengate looks great. Any chance of a little explanation and where i would remove the string and add it to the other list ? i have never used propertys before.
    Im Learning !!!!

  21. #21
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: £ $ % InStr Function

    Sure,

    You would need to loop through your list and call the validation method I posted for each list item. If it returns True, then add it to the other list and remove it from the original.

    e.g.
    VB Code:
    1. Dim i As Long
    2.     Do
    3.         If (hasIllegalChars(List1.List(i))) Then
    4.             List2.AddItem List1.List(i)
    5.             List1.RemoveItem i
    6.           Else
    7.             i = i + 1
    8.         End If
    9.     Loop Until (i = List1.ListCount - 1)

    Bear in mind a Property Get method, when used this way, is just like a function. I tend to use property gets when no actual action is taken (ie. a passive function), and functions when they actually do something as well as returning a result.

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: £ $ % InStr Function

    Code:
    Property Get hasIllegalChars(ByRef pszSearch As String) As Boolean
    Static chIllegals() As Byte, fRun As Boolean
    Dim chBuf() As Byte, I As Long, j As Long
    
        If (Not fRun) Then
            chIllegals = """£$%^&*()[}[{:;',#~<>?/|\"
            fRun = True
        End If
    
        chBuf = pszSearch
    
        For I = 0 To UBound(chBuf) Step 2
            For j = 0 To UBound(chIllegals)
                If (chBuf(I) = chIllegals(j)) Then
                    hasIllegalChars = True
                    Exit Property
                End If
            Next j
        Next I
    End Property
    
    Public Sub RemoveSymbols()
    Dim X As Long
    Dim Str As String
    
    X = lstIDs.ListCount - 1
    
    lstIDs.ListIndex = X
    
    Do Until X = -1
    If (hasIllegalChars(lstIDs.List(X))) Then
        lstSymbols.AddItem lstIDs.List(X)
        lstIDs.RemoveItem X
    Else
        I = I + 1
    End If
    
    Loop
    
    End Sub
    i used the above code and had the following in my list

    £$£kjffgdf
    gdfgfd%*%
    gdfgdf*&*
    ^&*(^df

    and it frooze on me.....
    Im Learning !!!!

  23. #23
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: £ $ % InStr Function

    That's becase you tried to make the loop go backwards, but left it as i = i + 1 instead of i = i -1

  24. #24
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: £ $ % InStr Function

    If you want to loop backwards (which is actually more efficient now I think about it):
    VB Code:
    1. Dim i As Long
    2. Do
    3.     If (hasIllegalChars(List1.List(i))) Then
    4.             List2.AddItem List1.List(i)
    5.             List1.RemoveItem i
    6.     End If
    7.     i = i -1
    8. Loop Until i = -1

    You obviously aren't using Option Explicit, otherwise it would have caught that you tried to increment I instead of X

  25. #25
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: £ $ % InStr Function

    Quote Originally Posted by penagate
    Hack, it would be more efficient to pass the string ByRef and the character as a Byte
    Why?

  26. #26
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: £ $ % InStr Function

    Quote Originally Posted by Hack
    Quote Originally Posted by penagate
    Hack, it would be more efficient to pass the string ByRef and the character as a Byte
    Why?
    When you pass something ByVal, you pass it directly to the function. In the case of a string that means copying the string contents and allocating two new (hidden) pointer variables, which is a two stack allocations and one heap allocation. Very slow. On the other hand when you pass a string ByRef, you pass only its address in memory to the function. That means only one pointer allocation is made and the string contents are not copied.

    I realise that looks like a load of gibberish so quite simply: if passing something to a function is like giving it an apple, ByVal is making a copy of the original apple and then giving it to the function, whereas ByRef is just telling it where the original one is.

    Passing the character as a byte instead of an integer, in Hack's algorithm, eliminates the implicit integer to byte cast when you compare the character code passed against each byte in the buffer. This is a significant saving because it reduces the time spent in every loop iteration.

Posting Permissions

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



Click Here to Expand Forum to Full Width