Results 1 to 8 of 8

Thread: [RESOLVED] Problem comparing two strings

  1. #1

    Thread Starter
    Lively Member kivisoft@'s Avatar
    Join Date
    Jul 2011
    Location
    Iran
    Posts
    96

    Resolved [RESOLVED] Problem comparing two strings

    Hey all

    For some reason I need to compare two letters. one stored in a variable and one is returned by MID$ function. The weird thing is that for some reason the compiler returns the comparison expression as false! even though the both strings are the same. I used ASC() function to get their character codes. the function returned 88(letter is X) for the one derived from MID$ which is correct code but for some reasons returns 10 (for the X letter stored in the variable) which returned absolutely nothing in Chr() function.

    What is going on here? Is there other ways to compare two strings? I used this expression:

    if var = MID$(........) then

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Problem comparing two strings

    Is there other ways to compare two strings?
    yes

    What is going on here?
    probably some other character in one of the strings, therefore not =
    chr(10) is a LF, is a non-printable character and does not show in most cases, but character after should appear on a new line

    check the length of both strings
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: Problem comparing two strings

    Code:
    if ( Len(String1) = Len(String2) ) And ( String1 = String2 ) Then ...
    Check length, and then check the actual strings data.
    if needed, Left() and Right() can also be used to retrieve the wanted String.

    I would also concider InStr() as a straight-foward solution.

  4. #4
    gibra
    Guest

    Re: Problem comparing two strings

    Quote Originally Posted by kivisoft@ View Post
    Hey all

    For some reason I need to compare two letters. one stored in a variable and one is returned by MID$ function. The weird thing is that for some reason the compiler returns the comparison expression as false! even though the both strings are the same. I used ASC() function to get their character codes. the function returned 88(letter is X) for the one derived from MID$ which is correct code but for some reasons returns 10 (for the X letter stored in the variable) which returned absolutely nothing in Chr() function.

    What is going on here? Is there other ways to compare two strings? I used this expression:

    if var = MID$(........) then
    Of course, your code is wrong.
    I think that the issue is that the string contains a CR+LF sequence, which their codes are 13+10, so MID() may search in wrong position.

  5. #5

    Thread Starter
    Lively Member kivisoft@'s Avatar
    Join Date
    Jul 2011
    Location
    Iran
    Posts
    96

    Re: Problem comparing two strings

    I ran a length check on both strings. the one stored in variable was 2 while the one derived by MID$ was 1! a Instr() check showed that there is indeed chr(10) in the string so I used replace function to replace the chr(10) with "". and it worked!

    it is funny that after so many years VB could still put me in trouble!!

    Thank you all so much!
    Code is the Void

  6. #6
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] Problem comparing two strings

    ASCII Table might prove useful!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  7. #7

    Thread Starter
    Lively Member kivisoft@'s Avatar
    Join Date
    Jul 2011
    Location
    Iran
    Posts
    96

    Re: [RESOLVED] Problem comparing two strings

    It is unreasonable that unprintable characters could affect the length of the string while technically it doesn't!
    Code is the Void

  8. #8
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Problem comparing two strings

    Quote Originally Posted by kivisoft@ View Post
    I ran a length check on both strings. the one stored in variable was 2 while the one derived by MID$ was 1! a Instr() check showed that there is indeed chr(10) in the string so I used replace function to replace the chr(10) with "". and it worked!

    it is funny that after so many years VB could still put me in trouble!!

    Thank you all so much!
    I think it might be because of the amount of data the New Line feed is actually putting into the string compared to "" which, I think might be the same as null?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

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