String comparison: Difference between revisions

Content added Content deleted
Line 16: Line 16:


=={{header|Ada}}==
=={{header|Ada}}==
Ada uses the usual comparison operators ("=" for equality, "/=" for not being equal, ect.) for strings. As Ada is strongly typed, comparing a string with an object from any other type (say, an integer or a floating point number) is a syntax error; one would have to make an explicit conversion. Comparisons are case sensitive. For case insensitive comparisons can be performed by employing some conversion operation, such as Ada.Characters.Handling.To_Lower from the standard library.
Ada uses the usual comparison operators ("=" for equality, "/=" for not being equal, ect.) for strings. As Ada is strongly typed, comparing a string with an object from any other type (say, an integer or a floating point number) is a syntax error; one would have to make an explicit conversion. Comparisons are case sensitive. Case insensitive comparisons can be performed by employing some conversion operation, such as Ada.Characters.Handling.To_Lower from the standard library.


<lang Ada>with Ada.Text_IO, Ada.Characters.Handling;
<lang Ada>with Ada.Text_IO, Ada.Characters.Handling;
Line 27: Line 27:
Put_Line
Put_Line
( """" & A & """ and """ & B & """: " &
( """" & A & """ and """ & B & """: " &
(if A = B then "equal, " elsif To_Lower(A)=To_Lower(B)
(if A = B then "equal, " elsif To_Lower(A) = To_Lower(B)
then "case-insensitive-equal, " else "not equal at all, ") &
then "case-insensitive-equal, " else "not equal at all, ") &
(if A /= B then "/=, " else "") &
(if A /= B then "/=, " else "") &
Line 55: Line 55:
"there" and "the": not equal at all, /=, after, (not <=), and >=.</pre>
"there" and "the": not equal at all, /=, after, (not <=), and >=.</pre>



=={{header|AWK}}==
=={{header|AWK}}==
In awk, the string matching operators are case sensitive, and the behaviour of the comparative operators depends on the locale being used. Be very careful with numeric strings, because whether they will be treated as numeric values or strings depends on how the values were obtained, and on which awk interpreter is being used. Numeric strings obtained from the input source, will be treated as numeric values, when compared with other strings containing numeric values. Strings valued defined as constants using doublequote enclosures will be treated as strings of characters and compared lexically. The behaviour of the operators when one value is considered to be numeric (eg from the input source), but the other value has been defined explicitly as a numeric string by using doublequote enclosures may also vary depending on which awk interpreter is being used.
In awk, the string matching operators are case sensitive, and the behaviour of the comparative operators depends on the locale being used. Be very careful with numeric strings, because whether they will be treated as numeric values or strings depends on how the values were obtained, and on which awk interpreter is being used. Numeric strings obtained from the input source, will be treated as numeric values, when compared with other strings containing numeric values. Strings valued defined as constants using doublequote enclosures will be treated as strings of characters and compared lexically. The behaviour of the operators when one value is considered to be numeric (eg from the input source), but the other value has been defined explicitly as a numeric string by using doublequote enclosures may also vary depending on which awk interpreter is being used.