String comparison: Difference between revisions

Add Ecstasy example
(Add Ecstasy example)
 
(5 intermediate revisions by 4 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,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,889 ⟶ 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,490 ⟶ 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