Results 1 to 9 of 9

Thread: Special Characters

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    4

    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

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

    Re: Special Characters

    Moved From The FAQ section

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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:
    1. Private mstrSpecials(4) As String
    2.  
    3. Private Sub Form_Load()
    4.    
    5.     mstrSpecials(0) = "!"
    6.     mstrSpecials(1) = "@"
    7.     mstrSpecials(2) = "#"
    8.     mstrSpecials(3) = "$"
    9.     mstrSpecials(4) = "%"
    10.     ' add more item if you need
    11.    
    12. End Sub
    13.  
    14. Private Function ContainsSpecial(ByVal strText As String) As Boolean
    15.     Dim j As Integer
    16.     Dim blnRet As Boolean
    17.    
    18.    
    19.     For j = 0 To UBound(mstrSpecials)
    20.         If InStr(1, strText, mstrSpecials(j)) <> 0 Then
    21.             blnRet = True
    22.             Exit For ' exit for after finding first special character
    23.         End If
    24.     Next
    25.    
    26.     ContainsSpecial = blnRet
    27. End Function



  4. #4
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    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","!&#163;@#")
    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.

  5. #5
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    4

    Re: Special Characters

    Thanx Guys.. It works :-)

  7. #7
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    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

  8. #8
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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.

  9. #9
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    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
  •  



Click Here to Expand Forum to Full Width