Compare two JS strings.

Syntax

bool
JS_CompareStrings(JSContext *cx, JSString *str1, JSString *str2,
                  int32_t *result);
Name Type Description
cx JSContext * The context to which both strings must belong.
str1 JSString * First string to compare.
str2 JSString * Second string to compare.
result int32_t * The out parameter. See description for value of *result.

Description

JS_CompareStrings compares two JS strings, str1 and str2. If the strings are identical in content and length, JS_CompareStrings stores 0 in *result. If str1 is less than str2, *result is less than 0. If str1 is greater than str2, *result is greater than 0.

On success the function returns JS_TRUE. On error, it returns JS_FALSE and the value in result is unchanged.

This function imposes a total order on all JavaScript strings, the same order imposed by the JavaScript string comparison operators (<, <=, >, >=), as described in ECMA 262-3 ยง 11.8.5. As the standard says:

The comparison of strings uses a simple lexicographic ordering on sequences of code point value values. There is no attempt to use the more complex, semantically oriented definitions of character or string equality and collating order defined in the Unicode specification. Therefore strings that are canonically equal according to the Unicode standard could test as unequal.

See Also