Erlang Central

Difference between revisions of "String Compare"

From ErlangCentral Wiki

 
Line 35:Line 35:
 
</code>
 
</code>
  
[[Category:CookBook]]
+
[[Category:CookBook]][[Category:StringRecipes]]

Revision as of 21:52, 3 September 2006

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