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|
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
Print Show (FUNC(_Eval(FUNC(_StrCmp
Print Show (FUNC(_Eval(FUNC(_StrCmp
Print Show (FUNC(_Eval(FUNC(_StrCmp
Print Show (FUNC(_Eval(FUNC(_StrCmp
Print
Line 1,291:
_Eval ' evaluate result
Param (1)
If a@ = 0 Then Return
If a@ > 0 Then Return
Return
Output:
<pre>
Line 1,865:
<syntaxhighlight lang="Delphi">
begin
if S1=S2 then Memo.Lines.Add(Format('"%s" is exactly equal to "%s"',[S1,S2]));
if S1<>S2 then Memo.Lines.Add(Format('"%s" is not equal to "%s"',[S1,S2]));
if S1<S2 then Memo.Lines.Add(Format('"%s" is less than "%s"',[S1,S2]));
if S1<=S2 then Memo.Lines.Add(Format('"%s" is less than or equal to "%s"',[S1,S2]));
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]));
if AnsiSameText(S1, S2) then Memo.Lines.Add(Format('"%s" is case insensitive equal to "%s"',[S1,S2]));
Memo.Lines.Add(Format('"%s" "%s" case sensitive different = %d',[S1,S2,AnsiCompareStr(S1,S2)]));
Memo.Lines.Add(Format('"%s" "%s" case insensitive different = %d',[S1,S2,AnsiCompareText(S1,S2)]));
Memo.Lines.Add(Format('"%s" is found at Index %d in "%s"',[S1,Pos(S1,S2),S2]));
end;
procedure
begin
ShowCompares(Memo,'Equal', 'Equal');
ShowCompares(Memo,'Case', 'CASE');
ShowCompares(Memo,'91', '1234');
ShowCompares(Memo,'boy', 'cowboy');
end;
Line 1,904 ⟶ 1,894:
{{out}}
<pre>
"Equal" is exactly equal to "Equal"
"Equal" is less than or equal to "Equal"
"Equal" is greater than or equal to "Equal"
"Equal" is case insensitive equal to "Equal"
"Equal" "Equal" case sensitive different = 0
"Equal" "Equal" case insensitive different = 0
"Equal" is found at Index 1 in "Equal"
"Case" is not equal to "CASE"
"Case" is greater than "CASE"
"Case" is greater than or equal to "CASE"
"Case" is case insensitive equal to "CASE"
"Case" "CASE" case sensitive different = -1
"Case" "CASE" case insensitive different = 0
"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}}==
<syntaxhighlight lang="easylang">
print "equal"
.
if
print "
.
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
dog == dog (case-insensitive)
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="
var compareStrings = Fn.new { |a, b, sens|
|