I have a problem in my program.
I have a condition that compare between 2 string:
(if (eq? (exp1) (exp2)))
When exp1 give me a string, and exp2 give me a string. To be sure, when I change the "eq?" to "=", it give me the next problem:
=: expects type <number> as 2nd argument, given: ie; other arguments were: ie.
When I'm running the program, the function doesnt enter to the first expression in the "if" function, and enter to the second one (meaning the condition is false).
What can I do?
Thank you.
[Function]string= compares two strings and is true if they are the same (corresponding characters are identical) but is false if they are not. The function equal calls string= if applied to two strings. The keyword arguments :start1 and :start2 are the places in the strings to start the comparison.
You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.
Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.
You use the relational (comparison) operators =, <>, ><, <, <=, =<, >, >=, and => to ascertain the relative positions of two strings in ASCII sort order. The result of comparing two strings in this way is a value of True, False, or NULL (if one of the operands is NULL).
According to the Equivalence predicates section of R6RS, you should be using equal?
, not eq?
, which instead tests whether its two arguments are exactly the same object (not two objects with the same value).
(eq? "a" "a") ; unspecified
(equal? "abc" "abc") ; #t
As knivil notes in a comment, the Strings section also mentions string=?
, specifically for string comparisons, which probably avoids doing a type check.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With