Results 1 to 8 of 8

Thread: Compare Strings by Case

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,247

    Compare Strings by Case

    I want to know if str1 = str2 and both are the same Case?

    Is this bulletproof ?
    Code:
    Function Samecase(x, y) As Boolean
        Samecase = InStr(1, x, y, vbBinaryCompare)
    End Function
    It seems to test ok but I may be missing something.

  2. #2
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,041

    Re: Compare Strings by Case

    Samecase("Test111", "Test1") returns True, so that makes it not bulletproof as far as I can tell what you are looking for.

    But I'm convinced I'm missing something here, because a simple x=y will return True if the strings are equal, which takes Case into account automatically.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,247

    Re: Compare Strings by Case

    [QUOTE= a simple x=y will return True if the strings are equal, which takes Case into account automatically.[/QUOTE]
    That was my initial thinking... but

    Code:
    x= "Man And Woman": y= "Man and Woman"
    ? x
    Man And Woman
    ? y
    Man and Woman
    ? x=y
    True
    Unless I have something set differently here ?

  4. #4
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,041

    Re: Compare Strings by Case

    The only thing I can think of is you have Option Compare Text set, which I believe will make string comparisons case insensitive.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,178

    Re: Compare Strings by Case

    Yes a simple string1=string2 will return true only if the strings are an exact match assuming that you have not added an Option Compare line to your code. The default is binary compare.

  6. #6
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,923

    Re: Compare Strings by Case

    Quote Originally Posted by AlexanderBB View Post
    I want to know if str1 = str2 and both are the same Case?
    Umhh - StrComp(..) ?

    Olaf

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    21,068

    Re: Compare Strings by Case

    Has anyone used Option Compare Text in ages?

    Option Compare Text feels like something left over from very early Microsoft Basics to help people keep running very old Basic programs dating back to the 1970s with its Teletypes and VDTs which commonly had no lower case.

    It feels like the sort of thing that could cause debugging nightmares. I'd rely on the default (Binary) or if really concerned somebody might go nuts I suppose I might specify Option Compare Binary explicitly. Then in the rare cases where I want a case-insensitive compare I'd use StrComp() as already suggested above.

  8. #8
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    2,037

    Re: Compare Strings by Case

    In my Immediate Window I get
    Code:
    x= "Man And Woman": y= "Man and Woman"
    ? x
    Man And Woman
    ? y
    Man and Woman
    ? x=y
    False
    There must be something non-standard set in your environment to make operator String=String case-insensitive, like Option Compare Text for instance.

    cheers,
    </wqw>

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