|
-
Oct 19th, 2005, 06:45 AM
#1
Thread Starter
Hyperactive Member
£ $ % 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.
-
Oct 19th, 2005, 06:51 AM
#2
Fanatic Member
Re: £ $ % InStr Function
Try the following you did not put a start pos in the instr function.
VB Code:
Private Sub Command1_Click()
Dim ID As String
ID = "Bomb£Drop"
If InStr(1, ID, "£") Then
Debug.Print "Yep it's there"
Else
Debug.Print "sorry not there"
End If
End Sub
Hope this helps!!
Useful Links
.Net
#Develop, GhostDoc, CodeKeep , be.PINVOKE, Good Code Snippet Site
Krypton Toolkit, XPCC / XP Common Controls, QSS Windows Forms Components
VB.COM
VB.Classic Help File, MB Controls, MZTools, ADO Stored Procedure Generator add-in,
-
Oct 19th, 2005, 06:57 AM
#3
Re: £ $ % InStr Function
This will not only tell you if it is there, but how many times it appears in the string.
VB Code:
Option Explicit
Private Function FindChar(ByVal pstrText As String, pintChar As Integer) As Long
Dim btyTextArray() As Byte
Dim i As Long
Dim lngCount As Long
btyTextArray() = pstrText
For i = 0 To UBound(btyTextArray) Step 2
If btyTextArray(i) = pintChar Then lngCount = lngCount + 1
Next
FindChar = lngCount
MsgBox lngCount
End Function
Private Sub Command1_Click()
Dim lngFind As Long
'163 is the ascii for £ sign
lngFind = FindChar(Text1.Text, 163)
End Sub
-
Oct 19th, 2005, 06:58 AM
#4
Thread Starter
Hyperactive Member
Re: £ $ % InStr Function
Ok seems to work but how would i check to see if ID contained " symbol.
it doesn't like """
-
Oct 19th, 2005, 06:59 AM
#5
Thread Starter
Hyperactive Member
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 ?
-
Oct 19th, 2005, 07:00 AM
#6
Re: £ $ % InStr Function
VB Code:
'In my post change
lngFind = FindChar(Text1.Text, 163)
'to
lngFind = FindChar(Text1.Text, 34)
-
Oct 19th, 2005, 07:02 AM
#7
Re: £ $ % InStr Function
 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.
-
Oct 19th, 2005, 07:06 AM
#8
Thread Starter
Hyperactive Member
Re: £ $ % InStr Function
In the immediate window type: ??
-
Oct 19th, 2005, 07:08 AM
#9
Re: £ $ % InStr Function
 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?
-
Oct 19th, 2005, 07:11 AM
#10
Thread Starter
Hyperactive Member
Re: £ $ % InStr Function
oh yeah i found it. I never have that window showing, Didn't know what it did.
Thanks.
-
Oct 19th, 2005, 07:50 AM
#11
Thread Starter
Hyperactive Member
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.
-
Oct 19th, 2005, 08:01 AM
#12
Re: £ $ % InStr Function
VB Code:
Private Sub Command1_Click()
Dim lngFind As Long
lngFind = FindChar(Text1.Text, 163)
If lngFind > 0 Then
'the character is there at least once
MsgBox "Yes sir. That sucker is in that string."
Else
'it is not found
MsgBox "Nope. I looked all over the place, and cant find it anywhere."
End If
End Sub
-
Oct 19th, 2005, 08:04 AM
#13
-
Oct 19th, 2005, 08:08 AM
#14
Thread Starter
Hyperactive Member
Re: £ $ % InStr Function
i leave the original findchar function as it is?
-
Oct 19th, 2005, 08:20 AM
#15
Re: £ $ % InStr Function
 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.
-
Oct 19th, 2005, 08:27 AM
#16
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:
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:
For i = 32 to 255
Debug.Print i,Chr$(i)
Next
This world is not my home. I'm just passing through.
-
Oct 19th, 2005, 08:56 AM
#17
Re: £ $ % InStr Function
Hack, it would be more efficient to pass the string ByRef and the character as a Byte
-
Oct 19th, 2005, 08:58 AM
#18
Thread Starter
Hyperactive Member
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.
-
Oct 19th, 2005, 09:07 AM
#19
Re: £ $ % InStr Function
VB 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 = [color=dimgray]"""£$%^&*()[}[{:;'[/color][color=dimgray],#~<>?/|\"[/color]
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
Last edited by penagate; Oct 19th, 2005 at 09:15 AM.
-
Oct 19th, 2005, 09:21 AM
#20
Thread Starter
Hyperactive Member
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.
-
Oct 19th, 2005, 09:29 AM
#21
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:
Dim i As Long
Do
If (hasIllegalChars(List1.List(i))) Then
List2.AddItem List1.List(i)
List1.RemoveItem i
Else
i = i + 1
End If
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.
-
Oct 19th, 2005, 09:36 AM
#22
Thread Starter
Hyperactive Member
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.....
-
Oct 19th, 2005, 09:42 AM
#23
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
-
Oct 19th, 2005, 09:46 AM
#24
Re: £ $ % InStr Function
If you want to loop backwards (which is actually more efficient now I think about it):
VB Code:
Dim i As Long
Do
If (hasIllegalChars(List1.List(i))) Then
List2.AddItem List1.List(i)
List1.RemoveItem i
End If
i = i -1
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
-
Oct 19th, 2005, 09:48 AM
#25
Re: £ $ % InStr Function
 Originally Posted by penagate
Hack, it would be more efficient to pass the string ByRef and the character as a Byte 
Why?
-
Oct 19th, 2005, 09:54 AM
#26
Re: £ $ % InStr Function
 Originally Posted by Hack
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|