Are identical strings ever not equal?

BlitzMax Forums/BlitzMax Beginners Area/Are identical strings ever not equal?

SculptureOfSoul(Posted 2007) [#1]
I'm sure this has been discussed but I can't find it and can't recall the answer

is it ever possible for two strings that are composed of the same characters to return false when comparing for equality?

A simple test
local s1:string = "string"
local s2:string = "string"
if(s1=s2)
print "true"
else
print "false"

shows that they are considered to be equal. I need to know though if any circumstances could make that return false.


Dreamora(Posted 2007) [#2]
No, on Strings = is doing per char equality check and therefor the same content in the string will result in true


ziggy(Posted 2007) [#3]
If they have the same characters, they're considered equal. when you make an object comparison MyObject1 = MyObject2, the comparison is based on memory addresses so two object with the same values in fields are different if they're not exactly pointing to the same instance. Strings are an exception to this mechanism. When two strings are compared, the comparison is not based on memory addresses but in their values. the exception also makes that when a string variable is asigned to another string variable, the value is copied instead of making them point to the same instance.

In general terms, think of string variables as if they were not objects, and use them in the same way you would use a Int, a Float, a Double or a Long numeric variable.


Canardian(Posted 2007) [#4]
I don't know how it's done in BlitzMax, but in C++ you can define class operators, like the comparison operator "==" for your own string class. If you don't define an "==" operator, the default operator is used, and it compares the object addresses.


ziggy(Posted 2007) [#5]
In BM you can not define operators. = compares object addresses when used against objects, single values when used against Int, Long, Float or Double, and object values when used against strings.


SculptureOfSoul(Posted 2007) [#6]
Thanks, that's exactly how I thought (and hoped) they behaved.