|
-
Dec 25th, 2011, 04:57 AM
#1
Thread Starter
New Member
Special Characters
Hi,
What is the VB 6.0 code for finding whether special characters (eg : !,#,$ etc) exists in a string?
Thanks and Regards
Janizar Akthar T
-
Dec 25th, 2011, 06:52 AM
#2
Re: Special Characters
Moved From The FAQ section
-
Dec 25th, 2011, 07:13 AM
#3
Re: Special Characters
If the characters you are searching for are too many, it is better to store them in an array then check if a string contain one of them, but if you are searching for few special character then use if statement for each character.
vb Code:
Private mstrSpecials(4) As String Private Sub Form_Load() mstrSpecials(0) = "!" mstrSpecials(1) = "@" mstrSpecials(2) = "#" mstrSpecials(3) = "$" mstrSpecials(4) = "%" ' add more item if you need End Sub Private Function ContainsSpecial(ByVal strText As String) As Boolean Dim j As Integer Dim blnRet As Boolean For j = 0 To UBound(mstrSpecials) If InStr(1, strText, mstrSpecials(j)) <> 0 Then blnRet = True Exit For ' exit for after finding first special character End If Next ContainsSpecial = blnRet End Function
-
Dec 25th, 2011, 07:29 AM
#4
Frenzied Member
Re: Special Characters
another option
use a string instead of the array
dim test as string
saves you remembering to to increase the size of the array when dimmed and no neet to redim if you inly use dim array() when you declare
the len(test) is the position of the last character in the string
and the string starts at position 1
so
Code:
Private Function ContainsSpecial(ByVal strText As String , byval test as string) As Boolean
Dim j As Integer
containsspecial=false
For j = 0 To len(test)
If InStr(1, strText,mid$(test,j,1)) <> 0 Then
containsspecial = True
Exit For ' exit for after finding first special character
End If
Next
End Function
so you simply call
ans=containsspecial("hello#mum","!£@#")
or some variables or constants
ans=(string_to_test,test_constant_string_set)
I would probably want the function as a public one rather than a private one
you can of course put the test string in the function, but that limits you to what you can test without having to change the function!
this is more "GENERIC" or "GENERAL SOLUTION" which I much prefer
oh yes... you could pass the test as an array too and turn the other solution in to a more General form
hope that helps
here to talk
Last edited by incidentals; Dec 25th, 2011 at 07:32 AM.
-
Dec 25th, 2011, 11:30 AM
#5
Re: Special Characters
Hmm, we already have the Like operator, so why not just use that?
Code:
Private Sub Command1_Click()
MsgBox "Has special chars = " _
& CStr(Text1.Text Like "*[!0-9A-Za-z]*")
Text1.SetFocus
End Sub
-
Dec 25th, 2011, 10:54 PM
#6
Thread Starter
New Member
Re: Special Characters
Thanx Guys.. It works :-)
-
Dec 26th, 2011, 01:03 PM
#7
Frenzied Member
Re: Special Characters
which one did you use?
have you rated any of the helpful replies?
and
dilenante can you expand on how exactly
CStr(Text1.Text Like "*[!0-9A-Za-z]*")
works for the newbies
its nice and concise and does (?) exactly?
here to talk
-
Dec 26th, 2011, 02:34 PM
#8
Re: Special Characters
Actually it breaks down to something like:
Code:
Dim SomeString As String
Dim HasSpecials As Boolean
SomeString = "something here"
HasSpecials = SomeString Like "*[!0-9A-Za-z]*"
The Like operator itself is documented in the VB6 manuals and online Help. This particular pattern however tests for a match with:
"Zero to many of any characters, followed by a character not in the ranges of "0" through "9" or "A" through "Z" or "a" through "z", followed by zero to many of any characters." So if we had a String value of "" for example there are no characters at all so the match fails (returns False).
If we had "xx" there are no specials so the match fails.
If we had "x.x" we have an "anything" and a special and a trailing "anything" character so the match succeeds (returns True).
If we had "." we have "zero anythings" and a special and "zero anythings" so the match succeeds.
Etc.
The * matches zero to many of any character.
The [] characters bracket a set of characters to be matched.
The ! means "not in this set."
The 0-9 means "characters 0 through 9" and A-Z means "A through Z" and so forth.
Pattern sets must list the character ranges in ascending order. So:
[A-Zz-a] will produce a runtime error because a comes before z.
To add a space as a valid "non special" character we could use the pattern:
Code:
HasSpecials = SomeString Like "*[! 0-9A-Za-z]*"
The manual has more details.
-
Dec 26th, 2011, 03:48 PM
#9
Frenzied Member
Re: Special Characters
that make a lot more sense to those who need to know how to match like that
thanks for the comments
here to talk
Tags for this Thread
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
|