Results 1 to 7 of 7

Thread: String Comparison Question.

  1. #1

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    45

    String Comparison Question.

    Hi,

    I have 2 string
    *************************
    dim a as string
    dim b as string

    a="cat"
    b="cat"
    ***************************
    if i want to compare both the string to see whether they are the same.How do i compare ??
    isit using
    *****************************
    if a=b then
    msgbox "SAME"
    else
    msgbox "NOT SAME"
    end if
    ***************************

    is my way of doing wrong ?? not is there other way ?? thanks.

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    that will work fine.

    your sample is case sensitive. If you want Cat = caT then you can do like this



    VB Code:
    1. If UCase(a) = UCase(b) Then
    2.     MsgBox "SAME"
    3. Else
    4.     MsgBox "NOT SAME"
    5. End If
    -= a peet post =-

  3. #3
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    use the StrComp function

    VB Code:
    1. Dim a As String
    2.     Dim b As String
    3.    
    4.     a = "cat"
    5.     b = "cAt"
    6.    
    7.     If StrComp(a, b, vbBinaryCompare) = 0 Then
    8.         MsgBox "match"
    9.     Else
    10.         MsgBox "Unmatch"
    11.     End If

    if the a dn b is not case sensitive, then compare with vbTextCompare as

    VB Code:
    1. Dim a As String
    2.     Dim b As String
    3.    
    4.     a = "cat"
    5.     b = "cat"
    6.    
    7.     If StrComp(a, b, vbTextCompare) = 0 Then
    8.         MsgBox "match"
    9.     Else
    10.         MsgBox "Unmatch"
    11.     End If

  4. #4

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    45
    OO thanks for all your reply

    But, even if my string a is "CAT" and the other is "cat"
    by using

    if a=b then
    msgbox "SAME"
    else
    msgbox "NOT SAME"
    end if

    i will get the msgbox "SAME" why ?? is it not case sensitive ??

  5. #5
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    it will return "NOT SAME"

  6. #6

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    45
    hmm, can i use "=" to compare 2 string ?? will it be accurate ??

  7. #7
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    it should be safe

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