PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Strings not equating-VBForums
Results 1 to 9 of 9

Thread: [RESOLVED] Strings not equating

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Resolved [RESOLVED] Strings not equating

    I'm running a string comparison on two strings that are identical in every conceivable way and yet the comparison is failing.

    One of the strings is s.TextFrame.TextRange.Text, where s is a Shape and the other is the value of a String.

    VB Code:
    1. If s.TextFrame.TextRange.Text = theSourceID Then
    2. ...

    I've used watches, message boxes and my eyes glued to the screen to verify that the strings are in fact identical. I tried copy the values of each string to a temporary string and comparing them and still it fails.
    I guess there must be some sort of invisible formatting on one of the strings which makes them fail using the = comparison. Is there a better way to compare these that
    a) ignores invisible formatting
    b) is case sensitive ?

    I'm looking at the help files for strcomp but I don't understand exactly how to use it from the examples given.

  2. #2
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    879

    Re: Strings not equating

    try something like:

    VB Code:
    1. If trim$(lcase$(s.TextFrame.TextRange.Text)) = trim$(lcase$(theSourceID)) Then 'Code goes here
    Visual Basic 6.0
    Visual C++ 5
    Delphi 5


  3. #3
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    879

    Re: Strings not equating

    If things aren't working properly still, then use a loop to msgbox the asc code of every character in both string. This will help you see if they are identical or not. BTW, there are Option vbStringCompare that you can use, but I forget how that goes.

    VB Code:
    1. Private Sub Command1_Click()
    2.   MsgBox StringAsc(theSourceID)
    3.   MsgBox StringAsc(s.TextFrame.TextRange.Text)
    4. End Sub
    5.  
    6. Public Function StringAsc(MyString As String) As String
    7.   Dim I As Long
    8.   Dim strTemp As String
    9.  
    10.   For I = 1 To Len(MyString)
    11.     strTemp = strTemp & Asc(Mid$(MyString, I, 1)) & " "
    12.   Next I
    13.  
    14.   StringAsc = Trim$(strTemp)
    15. End Function

    if all the numbers are equal, then there's no reason for the if statement to return false. meaning that there's other problems in your code that it's not even reaching the if statement.
    Visual Basic 6.0
    Visual C++ 5
    Delphi 5


  4. #4
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Strings not equating

    Quote Originally Posted by pickarooney
    I'm looking at the help files for strcomp but I don't understand exactly how to use it from the examples given.
    I wouldn't use StrComp for this particular situation. StrComp with vbTextCompare (or Option Compare Text) does more than just case-insensitive compare. Strings that don't match literally may compare equal. For example, ae matches the ligature . It isn't pure case-insensitive compare.

    Also bare in mind that StrComp(,, vbTextCompare) may work differently under different locales (regional settings). Your program might fail under a locale you haven't tested in.

    StrComp is best left for sorting. It's not the best function to compare for equality.

    Have you tried converting both strings to all one case and doing a compare using either the LCase or UCase functions?
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Re: Strings not equating

    Quote Originally Posted by VIP3R
    try something like:

    VB Code:
    1. If trim$(lcase$(s.TextFrame.TextRange.Text)) = trim$(lcase$(theSourceID)) Then 'Code goes here
    The comparison is still failing when I use this.

    Do I need to create some kind of a form to use the character-comparison code you posted?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Re: Strings not equating

    Ehm, forget that last question. So I added your StringAsc function and called it on both strings within my Sub. They all match up but the second string has one extra character, '13', which I think is a return character or line feed. If I snip that off the comparison should work.

    Thanks a million

    What's the best way to code "if the last character of aString = Chr(013) then remove it?"
    Last edited by pickarooney; Jul 11th, 2005 at 09:05 AM.

  7. #7
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    879

    Re: [RESOLVED] Strings not equating

    VB Code:
    1. If Right$(MyString, 1) = chr(13) Then MyString = Left$(MyString, Len(MyString) - 1)

    That's one method, I don't know if it's the best method though.
    Visual Basic 6.0
    Visual C++ 5
    Delphi 5


  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Re: [RESOLVED] Strings not equating

    I don't know why, but it doesn't work.

    I added this check:
    VB Code:
    1. If Right$(s.TextFrame.TextRange.Text, 1) = Chr(13) Then
    2.         s.TextFrame.TextRange.Text = Left$(s.TextFrame.TextRange.Text, Len(s.TextFrame.TextRange.Text) - 1)
    3.     End If
    4.    
    5.     MsgBox StringAsc(theSourceID)
    6.     MsgBox (theSourceID & "is " & Len(theSourceID) & "characters.")
    7.     MsgBox StringAsc(s.TextFrame.TextRange.Text)
    8.     MsgBox (s.TextFrame.TextRange.Text & "is " & Len(s.TextFrame.TextRange.Text) & "characters.")

    The message boxes output:
    1) The list of characters in the first string
    2) the message "string is 18 characters"
    3) The list of characters in the second string, which is the same as the first one but with an extra 13
    4) the message "string is 20 characters"

    What's strange is that the condition in the line of code you posted last seems to be true, as the clause after the if statement is activated when I skip through the program with F8. (I did change the variables to match those in my sub).
    I tried adding a msgbox before that line to check the length, and it's 20 characters long before and after the line to trim the return char.

    I'm confused.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Re: [RESOLVED] Strings not equating

    Maybe it's a compound last character, a mixture of carriage return and line-feed?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width