String comparison: Difference between revisions

(Added output for Unix Shell solution)
Line 15:
* [[Character matching]]
 
=={{header|AWKAda}}==
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.
 
<lang Ada>with Ada.Text_IO, Ada.Characters.Handling;
 
procedure String_Compare is
procedure Print_Comparison (A, B: String) is
use Ada.Text_IO, Ada.Characters.Handling;
begin
Put_Line
( """" & A & """ and """ & B & """: " &
(if A = B then "equal, " elsif To_Lower(A)=To_Lower(B)
then "case-insensitive-equal, " else "not equal at all, ") &
(if A /= B then "/=, " else "") &
(if A < B then "before, " else "") &
(if A > B then "after, " else "") &
(if A <= B then "<=, " else "(not <=), ") & "and " &
(if A >= B then ">=. " else "(not >=).") );
end Print_Comparison;
 
begin
Print_Comparison("this", "that");
Print_Comparison("that", "this");
Print_Comparison("THAT", "That");
Print_Comparison("this", "This");
Print_Comparison("this", "this");
Print_Comparison("the", "there");
Print_Comparison("there", "the");
end String_Compare;</lang>
 
{{out}}
<pre>"this" and "that": not equal at all, /=, after, (not <=), and >=.
"that" and "this": not equal at all, /=, before, <=, and (not >=).
"THAT" and "That": case-insensitive-equal, /=, before, <=, and (not >=).
"this" and "This": case-insensitive-equal, /=, after, (not <=), and >=.
"this" and "this": equal, <=, and >=.
"the" and "there": not equal at all, /=, before, <=, and (not >=).
"there" and "the": not equal at all, /=, after, (not <=), and >=.</pre>
 
 
=={{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.
 
Anonymous user