String operators

Provides a reference of string operators.

Table 1. String operators
Syntax Effect
string1 + string2

Returns a string containing the concatenation of string1 and string2.

Examples:

"Hello," + " world" –> "Hello, world"

When the operator + is used to add a string to a nonstring value, the nonstring value is first converted to a string.

Examples:

"Your age is " + 23 –> "Your age is 23"

23 + " is your age" –> "23 is your age"

string1 == string2

string1 != string2

The operator == returns the Boolean true if string1 and string2 are identical, and false otherwise. Two strings are identical if they have the same length and contain the same sequence of characters. The operator != is the converse of ==.

Examples:

"a string" == "a string" –> true

"a string" == "another string" –> false

"a string" == "A STRING" –> false

"a string" != "a string" –> false

"a string" != "another string" –> true

When the operators == and != are used to compare a string with a number, the string is first converted to a number and the two numbers are compared numerically.

Examples:

"12" == "+12" –> false

12 == "+12" –> true

string1 < string2

string1 <= string2

string1 > string2

string1 >= string2

The operator < returns true if string1 strictly precedes string2 lexicographically, and false otherwise. The operator <= returns true if string1 strictly precedes string2 lexicographically or is equal to it, and false otherwise; and so on.

Examples:

"abc" < "xyz" –> true

"a" < "abc" –> true

"xyz" < "abc" –> false

"abc" < "abc" –> false

"abc" > "xyz" –> false

"a" > "abc" –> false

"xyz" > "abc" –> true

Etc.

When one of these operators is used to compare a string with a number, the string is first converted to a number and the two numbers are compared numerically. In all other cases, the other argument is first converted to a string.

Examples:

"10" > "2" –> false

10 > "2" –> true

123 < "2" –> false

Hint: Autocasting may cause unexpected behavior.