Erlang Central

String Compare

Revision as of 21:56, 18 August 2006 by Cyberlync (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Problem

You need to compare two strings.

Solution

Use the comparison functions from the string library for equality:

1> string:equal("ABC", "abc").
false
2> string:equal("abc", "abc").
true

You can use the normal equality operators for sorting and other types of comparisons:

3> G = "ABC".
"ABC"
4> L = "abc".
"abc"
5> G > L.
false
6> G < L.
true
7> G >= L.
false
8> G =< L.
true

Note that lower case characters are considered greater than uppercase in these orderings.

Note that case insensitive versions of these functions are not available, though you can always use the httpd_util library to_lower or to_upper functions to put the two strings on an equal footing:

9> string:equal(httpd_util:to_lower("AAA"), "aaa").
true