Problem with string comparison

Djembefola

Active Member
Licensed User
Longtime User
in my app i have two strings s1 and s2. Both should have the same content and in my program i do a test with
B4X:
if s1 = s2 then...
This string comparison has always worked fine, but in the last weeks the comparison failed on some systems.
I'm not shure about the reasons: i have upgraded to b4a 2.0 and a lot of android systems get updates in these days.

if i look at the string content in b4a (with log, msgbox or with the tooltip popup help), they are identical.

But if i compare the strings with
B4X:
if (s1=s2) then ...

b4a tells me that both strings are different. :confused:

i have transformed both strings into byte arrays and one of both strings has three bytes more at the beginning. all other bytes are identical.
As a Workaround i strip the first three bytes of the longer byte array:

B4X:
'Workaround: strip first three bytes
   Dim d1() as Byte
   Dim d2() As Byte
   d1=s1.GetBytes(charset)
   d2=s2.GetBytes(charset)
   s1=BytesToString(d1,3,d1.Length-3,charset)
   s2=BytesToString(d2,0,d2.Length,charset)
   If s1<>s2 Then ...

This works, but i have no grasp, where the three bytes come from and what they mean...

Is this an error, or am i doing something wrong?
 
Last edited:

mc73

Well-Known Member
Licensed User
Longtime User
If you try
B4X:
s1.CompareTo (s2)
do you get 0 (means strings are identical)?
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Strings are a tricky thing in most recent languages. Strings used to be Byte Arrays or maybe ASCIIZ Type strings that were null terminated. Later they developed into a hybrid ASCIIZ with string length stored in the bytes right before the string data to add variable length capabilities. Now Strings are usually an Object/Class. There is usually something like a toString that is often implied and called when you get the value of the string. Sometimes depending on how you compare, use, or assign a string it may get the string text while other times you may get the String Object. Since each string usually points to a different object that contains the text along with the other methods that strings can do then they will/can be different even if they contain the same string.

This is why you need to use the compare method above to compare the actual text, otherwise there is a chance you are comparing the objects or even the memory address pointers to the objects in some languages.
 
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
I have tried both methods

B4X:
if s1.compareto(s2) = 0 then ...

and

B4X:
if s1=s2 then ...

In both cases the code after "then" is never executed.
:confused:
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
To eliminate any possible stray spaces or capital letters, you may want to try a comparison like shown below:
B4X:
Dim s1,s2 As String
s1="B4A" :s2="b4a "
If s1.Trim.ToLowerCase.Compareto(s2.Trim.ToLowerCase) = 0  Then
   Msgbox("equal","")
Else
 Msgbox("Not equal","")
End If
In the above case it returns equal.
 
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
I have used the debugger and inspected the two string variables very thoroughly. And i swear, i used my reading glasses :)

There is no visible difference between s1 and s2. length and characters are identical. the difference between the two strings can only be seen after transforming them into byte arrays. one of the two byte arrays has three additional bytes at the beginning. all other bytes have identical values.

The first string comes from a file as plain text, the second string is a result from a decryption with ByteConverter 1.1 and Encryption 1.1 Library.

The decryption routine has worked fine for a long time and suddenly it fails at times: the two strings were always identical before, but now they are different. I encountered the problem after releasing a new program version, written with b4a 2.0. The previous (well working) version was written in b4a 1.7. But the problem may also be related to an android 4.0 update.

:confused:
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
As Professor Klaus says in a case like this: 'Post your project as a zip file so others can check it out and see if they can help. IDE, 'File', 'Export as zip.
 
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
Three bytes always suggests a Byte order mark from a UTF-8 encoded file to me.

Thank you very much for this hint. i will check that.

The file is written by a b4a 1.6 application running on a android device (charset UTF8) , transferred via email, copied via Windows mtp to a sdcard and then opened by my program (charset UTF8).
Normally the file should not get a Byte Order Mark without user interaction (opening and saving, e.g. with notepad)
 
Upvote 0
Top