String comparison: Difference between revisions

Add Ecstasy example
No edit summary
(Add Ecstasy example)
 
(6 intermediate revisions by 5 users not shown)
Line 1,255:
<pre>Igual que la entrada de FreeBASIC.</pre>
==={{header|uBasic/4tH}}===
{{works with|R3R4}}
uBasic/4tH provides a builtin, case insensitive function to compare two strings, called <code>COMP()</code> which returns either a negative, zero or positive value, just like <code>strcmp()</code>. In order to compare two strings case sensitive, a user defined function is required.
<syntaxhighlight lang="text">Print "Case sensitive"
Print "=============="
Print Show (FUNC(_Eval(FUNC(_StrCmp (Dup ("Dog"), Dup ("Dog"))))))
Print Show (FUNC(_Eval(FUNC(_StrCmp (Dup ("Dog"), Dup ("Cat"))))))
Print Show (FUNC(_Eval(FUNC(_StrCmp (Dup ("Dog"), Dup ("Rat"))))))
Print Show (FUNC(_Eval(FUNC(_StrCmp (Dup ("Dog"), Dup ("dog"))))))
Print Show (FUNC(_Eval(FUNC(_StrCmp (Dup ("Dog"), Dup ("Pig"))))))
 
Print
Line 1,291:
_Eval ' evaluate result
Param (1)
If a@ = 0 Then Return (Dup ("Equal"))
If a@ > 0 Then Return (Dup ("Second before First"))
Return (Dup ("First before Second"))</syntaxhighlight>
Output:
<pre>
Line 1,865:
 
<syntaxhighlight lang="Delphi">
{String pack with control and extened chars}
 
const TestStr ='N'+#$01 +'o'+#$02 +'w'+#$03 +' '+#$04 +'i'+#$05 +'s'+#$06 +' '+#$07 +'t'+#$08 +'h'+#$09 +'e'+#$0A +' '+#$0B +'t'+#$0C +'i'+#$0D +'m'+#$0E +'e'+#$0F +' '+#$10 +'f'+#$11 +'o'+#$12 +'r'+#$13 +' '+#$14 +'a'+#$15 +'l'+#$16 +'l'+#$17 +' '+#$18 +'g'+#$19 +'o'+#$1A +'o'+#$1B +'d'+#$1C +' '+#$1D +'m'+#$1E +'e'+#$1F +'n'+#$80 +' '+#$81 +'t'+#$82 +'o'+#$83 +' '+#$84 +'c'+#$85 +'o'+#$86 +'m'+#$87 +'e'+#$88 +' '+#$89 +'t'+#$8A +'o'+#$8B +' '+#$8C +'t'+#$8D +'h'+#$8E +'e'+#$8F +' '+#$90 +'a'+#$91 +'i'+#$92 +'d'+#$93 +' '+#$94 +'o'+#$95 +'f'+#$96 +' '+#$97 +'t'+#$98 +'h'+#$99 +'e'+#$9A +' '+#$9B +'p'+#$9C +'a'+#$9D +'r'+#$9E +'t'+#$9F +'y'+#$A0;
 
functionprocedure StripControlsShowCompares(SMemo: string)TMemo; S1,S2: string);
{Strip control characters from string}
var I: integer;
begin
if S1=S2 then Memo.Lines.Add(Format('"%s" is exactly equal to "%s"',[S1,S2]));
Result:='';
if S1<>S2 then Memo.Lines.Add(Format('"%s" is not equal to "%s"',[S1,S2]));
for I:=1 to Length(S) do
if S1<S2 then Memo.Lines.Add(Format('"%s" is less than "%s"',[S1,S2]));
if byte(S[I])>=$20 then Result:=Result+S[I];
if S1<=S2 then Memo.Lines.Add(Format('"%s" is less than or equal to "%s"',[S1,S2]));
end;
if S1>S2 then Memo.Lines.Add(Format('"%s" is greater than "%s"',[S1,S2]));
 
if S1>=S2 then Memo.Lines.Add(Format('"%s" is greater than or equal to "%s"',[S1,S2]));
function StripExtended(S: string): string;
if AnsiSameText(S1, S2) then Memo.Lines.Add(Format('"%s" is case insensitive equal to "%s"',[S1,S2]));
{Strip extended characters from string}
Memo.Lines.Add(Format('"%s" "%s" case sensitive different = %d',[S1,S2,AnsiCompareStr(S1,S2)]));
var I: integer;
Memo.Lines.Add(Format('"%s" "%s" case insensitive different = %d',[S1,S2,AnsiCompareText(S1,S2)]));
begin
Memo.Lines.Add(Format('"%s" is found at Index %d in "%s"',[S1,Pos(S1,S2),S2]));
Result:='';
for I:=1 to Length(S) do
if byte(S[I])<$80 then Result:=Result+S[I];
end;
 
 
procedure StripStringShowStringCompares(Memo: TMemo);
begin
ShowCompares(Memo,'Equal', 'Equal');
Memo.Lines.Add('String full of controls and extended chars: ');
ShowCompares(Memo,'Case', 'CASE');
Memo.Lines.Add(TestStr);
ShowCompares(Memo,'91', '1234');
Memo.Lines.Add('String stripped of controls chars: ');
ShowCompares(Memo,'boy', 'cowboy');
Memo.Lines.Add(StripControls(TestStr));
Memo.Lines.Add('String stripped of extended chars: ');
Memo.Lines.Add(StripExtended(TestStr));
Memo.Lines.Add('String stripped of both control and extended chars: ');
Memo.Lines.Add(StripControls(StripExtended(TestStr)));
end;
 
Line 1,904 ⟶ 1,894:
{{out}}
<pre>
"Equal" is exactly equal to "Equal"
String full of controls and extended chars:
"Equal" is less than or equal to "Equal"
N�o�w� �i�s� �t�h e
"Equal" is greater than or equal to "Equal"
�t�i
"Equal" is case insensitive equal to "Equal"
m�e� �f�o�r� �a�l�l� �g�o�o�d� �m�e�n€ t‚oƒ „c…o†m‡eˆ ‰tŠo‹ ŒthŽe a‘i’d“ ”o•f– —t˜h™eš ›pœaržtŸy 
"Equal" "Equal" case sensitive different = 0
String stripped of controls chars:
"Equal" "Equal" case insensitive different = 0
Now is the time for all good men€ t‚oƒ „c…o†m‡eˆ ‰tŠo‹ ŒthŽe a‘i’d“ ”o•f– —t˜h™eš ›pœaržtŸy 
"Equal" is found at Index 1 in "Equal"
String stripped of extended chars:
"Case" is not equal to "CASE"
N�o�w� �i�s� �t�h e
"Case" is greater than "CASE"
�t�i
"Case" is greater than or equal to "CASE"
m�e� �f�o�r� �a�l�l� �g�o�o�d� �m�e�n to come to the aid of the party
"Case" is case insensitive equal to "CASE"
String stripped of both control and extended chars:
"Case" "CASE" case sensitive different = -1
Now is the time for all good men to come to the aid of the party
"Case" "CASE" case insensitive different = 0
Elapsed Time: 51.012 ms.
"Case" is found at Index 0 in "CASE"
"91" is not equal to "1234"
"91" is greater than "1234"
"91" is greater than or equal to "1234"
"91" "1234" case sensitive different = 1
"91" "1234" case insensitive different = 1
"91" is found at Index 0 in "1234"
"boy" is not equal to "cowboy"
"boy" is less than "cowboy"
"boy" is less than or equal to "cowboy"
"boy" "cowboy" case sensitive different = -1
"boy" "cowboy" case insensitive different = -1
"boy" is found at Index 4 in "cowboy"
Elapsed Time: 41.211 ms.
 
</pre>
 
 
=={{header|Dyalect}}==
Line 1,955 ⟶ 1,958:
 
=={{header|EasyLang}}==
EasyLang only has operators for testing the equality and inequality of strings.
<syntaxhighlight lang="easylang">
stringAa$ = "Stringhello"
stringBif a$ = "stringhello"
print "equal"
stringC$ = "string"
if stringB$ = stringC$
print "\"" & stringB$ & "\" is equal to \"" & stringC$ & "\""
.
if stringAa$ <> stringB$"hello2"
print "\"" & stringA$ & "\" is not equal to \"" & stringB$ & "\""
.
if strcmp a$ "hello" = 0
print "equal"
.
if strcmp a$ "world" < 0
print "lexically before"
.
if number "10" > number "2"
print "numerically after"
.
</syntaxhighlight>
 
=={{header|Ecstasy}}==
In Ecstasy, strings are objects, like all values. Any class, including classes like <code>Int</code> and <code>String</code>, can provide operator support by annotating the methods that represent those operators. The result is simple uniformity of how types are defined, including their operators. String comparisons rely on these operators:
 
<syntaxhighlight lang="ecstasy">
module StringComparisons {
void run() {
@Inject Console console;
import ecstasy.collections.CaseInsensitive;
 
String[] tests = ["dog", "cat", "Dog"];
String s1 = tests[0];
for (String s2 : tests) {
// Comparing two strings for exact equality
if (s1 == s2) {
console.print($"{s1} == {s2}");
}
 
// Comparing two strings for inequality
if (s1 != s2) {
console.print($"{s1} != {s2}");
}
 
// Comparing two strings to see if one is lexically ordered
// before the other
if (s1 < s2) {
console.print($"{s1} < {s2}");
}
 
// Comparing two strings to see if one is lexically ordered
// after the other
if (s1 > s2) {
console.print($"{s1} > {s2}");
}
 
// How to achieve both case sensitive comparisons and case
// insensitive comparisons within the language
 
if (CaseInsensitive.areEqual(s1, s2)) {
console.print($"{s1} == {s2} (case-insensitive)");
} else {
console.print($"{s1} != {s2} (case-insensitive)");
}
 
switch (CaseInsensitive.compare(s1, s2)) {
case Lesser:
console.print($"{s1} < {s2} (case-insensitive)");
break;
case Equal:
// already covered this one above
assert CaseInsensitive.areEqual(s1, s2);
break;
case Greater:
console.print($"{s1} > {s2} (case-insensitive)");
break;
}
}
}
}
</syntaxhighlight>
 
{{out}}
<pre>
dog == dog
"string" is equal to "string"
dog == dog (case-insensitive)
"String" is not equal to "string"
dog != cat
dog > cat
dog != cat (case-insensitive)
dog > cat (case-insensitive)
dog != Dog
dog > Dog
dog == Dog (case-insensitive)
</pre>
 
Line 3,886 ⟶ 3,963:
</syntaxhighlight>
 
=={{header|RPL}}==
Equality can be tested either with <code>==</code> or <code>SAME</code> operators:
"ab" "abc" ==
returns 0 (false).
 
To test inequality:
"ab" "abc" ≠
returns 1 (true).
 
Lexical order can be checked with <code><</code>, <code>≤</code>, <code>></code> or <code> ≥</code> operators.
"ab" "abc" ≤
returns also 1 (true).
All the above tests are case-sensitive.
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">method_names = [:==,:!=, :>, :>=, :<, :<=, :<=>, :casecmp]
Line 4,487 ⟶ 4,577:
 
Case insensitive comparisons can be achieved by converting both strings to the same case before the comparisons are made.
<syntaxhighlight lang="ecmascriptwren">import "./str" for Str
 
var compareStrings = Fn.new { |a, b, sens|
162

edits