String comparison: Difference between revisions
Content added Content deleted
(Added Wren) |
|||
Line 3,513: | Line 3,513: | ||
# This is false. Strings are not coerced to numbers and vice-versa. |
# This is false. Strings are not coerced to numbers and vice-versa. |
||
== '3' 3 -- io.writeln io.stdout;</lang> |
== '3' 3 -- io.writeln io.stdout;</lang> |
||
=={{header|Wren}}== |
|||
{{libheader|Wren-str}} |
|||
The only string comparisons built into the language itself are equality and inequality. However, the above module enables us to do full lexicographical comparisons of any strings, including numeric strings, by comparing their corresponding Unicode codepoints. |
|||
Case insensitive comparisons can be achieved by converting both strings to the same case before the comparisons are made. |
|||
<lang ecmascript>import "/str" for Str |
|||
var compareStrings = Fn.new { |a, b, sens| |
|||
System.write("Comparing '%(a)' and '%(b)', ") |
|||
var c |
|||
var d |
|||
if (sens) { |
|||
System.print("case sensitively:") |
|||
c = a |
|||
d = b |
|||
} else { |
|||
System.print("case insensitively:") |
|||
c = Str.lower(a) |
|||
d = Str.lower(b) |
|||
} |
|||
System.print(" %(a) < %(b) -> %(Str.lt(c, d))") |
|||
System.print(" %(a) > %(b) -> %(Str.gt(c, d))") |
|||
System.print(" %(a) == %(b) -> %(c == d)") |
|||
System.print(" %(a) != %(b) -> %(c != d)") |
|||
System.print(" %(a) <= %(b) -> %(Str.le(c, d))") |
|||
System.print(" %(a) >= %(b) -> %(Str.ge(c, d))") |
|||
System.print() |
|||
} |
|||
compareStrings.call("cat", "dog", true) |
|||
compareStrings.call("Rat", "RAT", true) |
|||
compareStrings.call("Rat", "RAT", false) |
|||
compareStrings.call("1100", "200", true)</lang> |
|||
{{out}} |
|||
<pre> |
|||
Comparing 'cat' and 'dog', case sensitively: |
|||
cat < dog -> true |
|||
cat > dog -> false |
|||
cat == dog -> false |
|||
cat != dog -> true |
|||
cat <= dog -> true |
|||
cat >= dog -> false |
|||
Comparing 'Rat' and 'RAT', case sensitively: |
|||
Rat < RAT -> false |
|||
Rat > RAT -> true |
|||
Rat == RAT -> false |
|||
Rat != RAT -> true |
|||
Rat <= RAT -> false |
|||
Rat >= RAT -> true |
|||
Comparing 'Rat' and 'RAT', case insensitively: |
|||
Rat < RAT -> false |
|||
Rat > RAT -> false |
|||
Rat == RAT -> true |
|||
Rat != RAT -> false |
|||
Rat <= RAT -> true |
|||
Rat >= RAT -> true |
|||
Comparing '1100' and '200', case sensitively: |
|||
1100 < 200 -> true |
|||
1100 > 200 -> false |
|||
1100 == 200 -> false |
|||
1100 != 200 -> true |
|||
1100 <= 200 -> true |
|||
1100 >= 200 -> false |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |