Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,3 @@
---
from: http://rosettacode.org/wiki/String_comparison
note: Basic Data Operations

View file

@ -0,0 +1,29 @@
{{basic data operation}}
[[Category:Basic language learning]]
[[Category:Simple]]
;Task:
Demonstrate how to compare two strings from within the language and how to achieve a lexical comparison.
The task should demonstrate:
* Comparing two strings for exact equality
* Comparing two strings for inequality (i.e., the inverse of exact equality)
* Comparing two strings to see if one is lexically ordered before than the other
* Comparing two strings to see if one is lexically ordered after than the other
* How to achieve both case sensitive comparisons and case insensitive comparisons within the language
* How the language handles comparison of numeric strings if these are not treated lexically
* Demonstrate any other kinds of string comparisons that the language provides, particularly as it relates to your type system.
For example, you might demonstrate the difference between generic/polymorphic comparison and coercive/allomorphic comparison if your language supports such a distinction.
<br>
Here "generic/polymorphic" comparison means that the function or operator you're using doesn't always do string comparison, but bends the actual semantics of the comparison depending on the types one or both arguments; with such an operator, you achieve string comparison only if the arguments are sufficiently string-like in type or appearance.
In contrast, a "coercive/allomorphic" comparison function or operator has fixed string-comparison semantics regardless of the argument type; &nbsp; instead of the operator bending, it's the arguments that are forced to bend instead and behave like strings if they can, &nbsp; and the operator simply fails if the arguments cannot be viewed somehow as strings. &nbsp; A language may have one or both of these kinds of operators; &nbsp; see the Raku entry for an example of a language with both kinds of operators.
{{Template:Strings}}
<br><br>

View file

@ -0,0 +1,11 @@
F compare(a, b)
I a < b {print('#.' is strictly less than '#.'.format(a, b))}
I a <= b {print('#.' is less than or equal to '#.'.format(a, b))}
I a > b {print('#.' is strictly greater than '#.'.format(a, b))}
I a >= b {print('#.' is greater than or equal to '#.'.format(a, b))}
I a == b {print('#.' is equal to '#.'.format(a, b))}
I a != b {print('#.' is not equal to '#.'.format(a, b))}
compare(YUP, YUP)
compare(BALL, BELL)
compare(24, 123)

View file

@ -0,0 +1,180 @@
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program comparString64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
/*******************************************/
/* Initialized data */
/*******************************************/
.data
szMessStringEqu: .asciz "The strings are equals.\n"
szMessStringNotEqu: .asciz "The strings are not equals.\n"
szCarriageReturn: .asciz "\n"
szString1: .asciz "ABCDE"
szString2: .asciz "ABCDE"
szString3: .asciz "ABCFG"
szString4: .asciz "ABC"
szString5: .asciz "abcde"
/*******************************************/
/* UnInitialized data /
/*******************************************/
.bss
/*******************************************/
/* code section */
/*******************************************/
.text
.global main
main: // entry of program
ldr x0,qAdrszString1
ldr x1,qAdrszString2
bl Comparaison
ldr x0,qAdrszString1
ldr x1,qAdrszString3
bl Comparaison
ldr x0,qAdrszString1
ldr x1,qAdrszString4
bl Comparaison
// case sensitive comparisons ABCDE et abcde
ldr x0,qAdrszString1
ldr x1,qAdrszString5
bl Comparaison
// case insensitive comparisons ABCDE et abcde
ldr x0,qAdrszString1
ldr x1,qAdrszString5
bl comparStringsInsensitive
cbnz x0,1f
ldr x0,qAdrszMessStringEqu
bl affichageMess
b 2f
1:
ldr x0,qAdrszMessStringNotEqu
bl affichageMess
2:
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrszString1: .quad szString1
qAdrszString2: .quad szString2
qAdrszString3: .quad szString3
qAdrszString4: .quad szString4
qAdrszString5: .quad szString5
qAdrszMessStringEqu: .quad szMessStringEqu
qAdrszMessStringNotEqu: .quad szMessStringNotEqu
qAdrszCarriageReturn: .quad szCarriageReturn
/*********************************************/
/* comparaison */
/*********************************************/
/* x0 contains address String 1 */
/* x1 contains address String 2 */
Comparaison:
stp x1,lr,[sp,-16]! // save registers
bl comparStrings
cbnz x0,1f
ldr x0,qAdrszMessStringEqu
bl affichageMess
b 2f
1:
ldr x0,qAdrszMessStringNotEqu
bl affichageMess
2:
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/************************************/
/* Strings case sensitive comparisons */
/************************************/
/* x0 et x1 contains the address of strings */
/* return 0 in x0 if equals */
/* return -1 if string x0 < string x1 */
/* return 1 if string x0 > string x1 */
comparStrings:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
mov x2,#0 // counter
1:
ldrb w3,[x0,x2] // byte string 1
ldrb w4,[x1,x2] // byte string 2
cmp x3,x4
blt 2f
bgt 3f
cbz x3,4f // 0 end string
add x2,x2,1 // else add 1 in counter
b 1b // and loop */
2:
mov x0,-1 // lower
b 100f
3:
mov x0,1 // higher
b 100f
4:
mov x0,0 // equal
100:
ldp x4,x5,[sp],16 // restaur 2 registers
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/************************************/
/* Strings case insensitive comparisons */
/************************************/
/* x0 et x1 contains the address of strings */
/* return 0 in x0 if equals */
/* return -1 if string x0 < string x1 */
/* return 1 if string x0 > string x1 */
comparStringsInsensitive:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
mov x2,#0 // counter
1:
ldrb w3,[x0,x2] // byte string 1
ldrb w4,[x1,x2] // byte string 2
// majuscules --> minuscules byte 1
cmp x3,65
blt 2f
cmp x3,90
bgt 2f
add x3,x3,32
2: // majuscules --> minuscules byte 2
cmp x4,65
blt 3f
cmp x4,90
bgt 3f
add x4,x4,32
3:
cmp x3,x4
blt 4f
bgt 5f
cbz x3,6f // 0 end string
add x2,x2,1 // else add 1 in counter
b 1b // and loop
4:
mov x0,-1 // lower
b 100f
5:
mov x0,1 // higher
b 100f
6:
mov x0,0 // equal
100:
ldp x4,x5,[sp],16 // restaur 2 registers
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"

View file

@ -0,0 +1,67 @@
STRING a := "abc ", b := "ABC ";
# when comparing strings, Algol 68 ignores trailing blanks #
# so e.g. "a" = "a " is true #
# test procedure, prints message if condition is TRUE #
PROC test = ( BOOL condition, STRING message )VOID:
IF condition THEN print( ( message, newline ) ) FI;
# equality? #
test( a = b, "a = b" );
# inequality? #
test( a /= b, "a not = b" );
# lexically ordered before? #
test( a < b, "a < b" );
# lexically ordered after? #
test( a > b, "a > b" );
# Algol 68's builtin string comparison operators are case-sensitive. #
# To perform case insensitive comparisons, procedures or operators #
# would need to be written #
# e.g. #
# compare two strings, ignoring case #
# Note the "to upper" PROC is an Algol 68G extension #
# It could be written in standard Algol 68 (assuming ASCII) as e.g. #
# PROC to upper = ( CHAR c )CHAR: #
# IF c < "a" OR c > "z" THEN c #
# ELSE REPR ( ( ABS c - ABS "a" ) + ABS "A" ) FI; #
PROC caseless comparison = ( STRING a, b )INT:
BEGIN
INT a max = UPB a, b max = UPB b;
INT a pos := LWB a, b pos := LWB b;
INT result := 0;
WHILE result = 0
AND ( a pos <= a max OR b pos <= b max )
DO
CHAR a char := to upper( IF a pos <= a max THEN a[ a pos ] ELSE " " FI );
CHAR b char := to upper( IF b pos <= b max THEN b[ b pos ] ELSE " " FI );
result := ABS a char - ABS b char;
a pos +:= 1;
b pos +:= 1
OD;
IF result < 0 THEN -1 ELIF result > 0 THEN 1 ELSE 0 FI
END ; # caseless comparison #
# compare two strings for equality, ignoring case #
PROC equal ignoring case = ( STRING a, b )BOOL: caseless comparison( a, b ) = 0;
# similar procedures for inequality and lexical ording ... #
test( equal ignoring case( a, b ), "a = b (ignoring case)" );
# Algol 68 is strongly typed - strings cannot be compared to e.g. integers #
# unless procedures or operators are written, e.g. #
# e.g. OP = = ( STRING a, INT b )BOOL: a = whole( b, 0 ); #
# OP = = ( INT a, STRING b )BOOL: b = a; #
# etc. #
# Algol 68 also has <= and >= comparison operators for testing for #
# "lexically before or equal" and "lexically after or equal" #
test( a <= b, "a <= b" );
test( a >= b, "a >= b" );
# there are no other forms of string comparison builtin to Algol 68 #

View file

@ -0,0 +1,67 @@
begin
string(10) a;
string(12) b;
a := "abc";
b := "ABC";
% when comparing strings, Algol W ignores trailing blanks %
% so e.g. "a" = "a " is true %
% equality? %
if a = b then write( "a = b" );
% inequality? %
if a not = b then write( "a not = b" );
% lexically ordered before? %
if a < b then write( "a < b" );
% lexically ordered after? %
if a > b then write( "a > b" );
% Algol W string comparisons are case-sensitive. To perform case %
% insensitive comparisons, procedures would need to be written %
% e.g. as in the following block (assuming the character set is ASCII) %
begin
% convert a character to upper-case %
integer procedure toupper( integer value c ) ;
if c < decode( "a" ) or c > decode( "z" ) then c
else ( c - decode( "a" ) ) + decode( "A" );
% compare two strings, ignoring case %
% note that strings can be at most 256 characters long in Algol W %
integer procedure caselessComparison ( string(256) value a, b ) ;
begin
integer comparisonResult, pos;
comparisonResult := pos := 0;
while pos < 256 and comparisonResult = 0 do begin
comparisonResult := toupper( decode( a(pos//1) ) )
- toupper( decode( b(pos//1) ) );
pos := pos + 1
end;
if comparisonResult < 0 then -1
else if comparisonResult > 0 then 1
else 0
end caselessComparison ;
% compare two strings for equality, ignoring case %
logical procedure equalIgnoringCase ( string(256) value a, b ) ;
( caselessComparison( a, b ) = 0 );
% similar procedures for inequality and lexical ording ... %
if equalIgnoringCase( a, b ) then write( "a = b (ignoring case)" )
end caselessComparison ;
% Algol W is strongly typed - strings cannot be compared to e.g. integers %
% e.g. "if a = 23 then ..." would be a syntax error %
% Algol W also has <= and >= comparison operators for testing for %
% "lexically before or equal" and "lexically after or equal" %
if a <= b then write( "a <= b" );
if a >= b then write( "a >= b" );
% there are no other forms of string comparison builtin to Algol W %
end.

View file

@ -0,0 +1,179 @@
/* ARM assembly Raspberry PI */
/* program comparString.s */
/* Constantes */
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
/* Initialized data */
.data
szMessStringEqu: .asciz "The strings are equals.\n"
szMessStringNotEqu: .asciz "The strings are not equals.\n"
szCarriageReturn: .asciz "\n"
szString1: .asciz "ABCDE"
szString2: .asciz "ABCDE"
szString3: .asciz "ABCFG"
szString4: .asciz "ABC"
szString5: .asciz "abcde"
/* UnInitialized data */
.bss
/* code section */
.text
.global main
main: /* entry of program */
push {fp,lr} /* saves 2 registers */
ldr r0,iAdrszString1
ldr r1,iAdrszString2
bl Comparaison
ldr r0,iAdrszString1
ldr r1,iAdrszString3
bl Comparaison
ldr r0,iAdrszString1
ldr r1,iAdrszString4
bl Comparaison
@ case sensitive comparisons ABCDE et abcde
ldr r0,iAdrszString1
ldr r1,iAdrszString5
bl Comparaison
@ case insensitive comparisons ABCDE et abcde
ldr r0,iAdrszString1
ldr r1,iAdrszString5
bl comparStringsInsensitive
cmp r0,#0
bne 1f
ldr r0,iAdrszMessStringEqu
bl affichageMess
b 2f
1:
ldr r0,iAdrszMessStringNotEqu
bl affichageMess
2:
100: /* standard end of the program */
mov r0, #0 @ return code
pop {fp,lr} @restaur 2 registers
mov r7, #EXIT @ request to exit program
swi 0 @ perform the system call
iAdrszString1: .int szString1
iAdrszString2: .int szString2
iAdrszString3: .int szString3
iAdrszString4: .int szString4
iAdrszString5: .int szString5
iAdrszMessStringEqu: .int szMessStringEqu
iAdrszMessStringNotEqu: .int szMessStringNotEqu
iAdrszCarriageReturn: .int szCarriageReturn
/*********************************************/
/* comparaison */
/*********************************************/
/* r0 contains address String 1 */
/* r1 contains address String 2 */
Comparaison:
push {fp,lr} /* save registres */
bl comparStrings
cmp r0,#0
bne 1f
ldr r0,iAdrszMessStringEqu
bl affichageMess
b 2f
1:
ldr r0,iAdrszMessStringNotEqu
bl affichageMess
2:
pop {fp,lr} /* restaur des 2 registres */
bx lr /* return */
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {fp,lr} /* save registres */
push {r0,r1,r2,r7} /* save others registers */
mov r2,#0 /* counter length */
1: /* loop length calculation */
ldrb r1,[r0,r2] /* read octet start position + index */
cmp r1,#0 /* if 0 its over */
addne r2,r2,#1 /* else add 1 in the length */
bne 1b /* and loop */
/* so here r2 contains the length of the message */
mov r1,r0 /* address message in r1 */
mov r0,#STDOUT /* code to write to the standard output Linux */
mov r7, #WRITE /* code call system "write" */
swi #0 /* call systeme */
pop {r0,r1,r2,r7} /* restaur others registers */
pop {fp,lr} /* restaur des 2 registres */
bx lr /* return */
/************************************/
/* Strings case sensitive comparisons */
/************************************/
/* r0 et r1 contains the address of strings */
/* return 0 in r0 if equals */
/* return -1 if string r0 < string r1 */
/* return 1 if string r0 > string r1 */
comparStrings:
push {r1-r4} /* save des registres */
mov r2,#0 /* counter */
1:
ldrb r3,[r0,r2] /* byte string 1 */
ldrb r4,[r1,r2] /* byte string 2 */
cmp r3,r4
movlt r0,#-1 /* small */
movgt r0,#1 /* greather */
bne 100f /* not equals */
cmp r3,#0 /* 0 end string */
moveq r0,#0 /* equals */
beq 100f /* end string */
add r2,r2,#1 /* else add 1 in counter */
b 1b /* and loop */
100:
pop {r1-r4}
bx lr
/************************************/
/* Strings case insensitive comparisons */
/************************************/
/* r0 et r1 contains the address of strings */
/* return 0 in r0 if equals */
/* return -1 if string r0 < string r1 */
/* return 1 if string r0 > string r1 */
comparStringsInsensitive:
push {r1-r4} /* save des registres */
mov r2,#0 /* counter */
1:
ldrb r3,[r0,r2] /* byte string 1 */
ldrb r4,[r1,r2] /* byte string 2 */
@ majuscules --> minuscules byte 1
cmp r3,#65
blt 2f
cmp r3,#90
bgt 2f
add r3,#32
2: @ majuscules --> minuscules byte 2
cmp r4,#65
blt 3f
cmp r4,#90
bgt 3f
add r4,#32
3:
cmp r3,r4
movlt r0,#-1 /* small */
movgt r0,#1 /* greather */
bne 100f /* not equals */
cmp r3,#0 /* 0 end string */
moveq r0,#0 /* equal */
beq 100f /* end strings */
add r2,r2,#1 /* else add 1 in counter */
b 1b /* and loop */
100:
pop {r1-r4}
bx lr /* end procedure */

View file

@ -0,0 +1,17 @@
BEGIN {
a="BALL"
b="BELL"
if (a == b) { print "The strings are equal" }
if (a != b) { print "The strings are not equal" }
if (a > b) { print "The first string is lexically after than the second" }
if (a < b) { print "The first string is lexically before than the second" }
if (a >= b) { print "The first string is not lexically before than the second" }
if (a <= b) { print "The first string is not lexically after than the second" }
# to make a case insensitive comparison convert both strings to the same lettercase:
a="BALL"
b="ball"
if (tolower(a) == tolower(b)) { print "The first and second string are the same disregarding letter case" }
}

View file

@ -0,0 +1,106 @@
PROC TestEqual(CHAR ARRAY s1,s2)
INT res
PrintF("""%S"" and ""%S"" are equal: ",s1,s2)
IF SCompare(s1,s2)=0 THEN
PrintE("True")
ELSE
PrintE("False")
FI
RETURN
PROC TestInequal(CHAR ARRAY s1,s2)
INT res
PrintF("""%S"" and ""%S"" are inequal: ",s1,s2)
IF SCompare(s1,s2)#0 THEN
PrintE("True")
ELSE
PrintE("False")
FI
RETURN
PROC TestBefore(CHAR ARRAY s1,s2)
INT res
PrintF("""%S"" is before ""%S"": ",s1,s2)
IF SCompare(s1,s2)<0 THEN
PrintE("True")
ELSE
PrintE("False")
FI
RETURN
PROC TestAfter(CHAR ARRAY s1,s2)
INT res
PrintF("""%S"" is after ""%S"": ",s1,s2)
IF SCompare(s1,s2)>0 THEN
PrintE("True")
ELSE
PrintE("False")
FI
RETURN
PROC TestNumEqual(CHAR ARRAY s1,s2)
INT v1,v2
PrintF("""%S"" and ""%S"" are equal: ",s1,s2)
v1=ValI(s1) v2=ValI(s2)
IF v1=v2 THEN
PrintE("True")
ELSE
PrintE("False")
FI
RETURN
PROC TestNumInequal(CHAR ARRAY s1,s2)
INT v1,v2
PrintF("""%S"" and ""%S"" are inequal: ",s1,s2)
v1=ValI(s1) v2=ValI(s2)
IF v1#v2 THEN
PrintE("True")
ELSE
PrintE("False")
FI
RETURN
PROC TestNumBefore(CHAR ARRAY s1,s2)
INT v1,v2
PrintF("""%S"" is before ""%S"": ",s1,s2)
v1=ValI(s1) v2=ValI(s2)
IF v1<v2 THEN
PrintE("True")
ELSE
PrintE("False")
FI
RETURN
PROC TestNumAfter(CHAR ARRAY s1,s2)
INT v1,v2
PrintF("""%S"" is after ""%S"": ",s1,s2)
v1=ValI(s1) v2=ValI(s2)
IF v1>v2 THEN
PrintE("True")
ELSE
PrintE("False")
FI
RETURN
PROC Main()
PrintE("Lexical comparison:")
TestEqual("abcd","Abcd")
TestInequal("abcd","Abcd")
TestBefore("abcd","Abcd")
TestAfter("abcd","Abcd")
PutE()
PrintE("Numerical comparison:")
TestNumEqual("1234","99876")
TestNumInequal("1234","99876")
TestNumBefore("1234","99876")
TestNumAfter("1234","99876")
RETURN

View file

@ -0,0 +1,28 @@
with Ada.Text_IO, Ada.Strings.Equal_Case_Insensitive;
procedure String_Compare is
procedure Print_Comparison (A, B : String) is
begin
Ada.Text_IO.Put_Line
("""" & A & """ and """ & B & """: " &
(if A = B then
"equal, "
elsif Ada.Strings.Equal_Case_Insensitive (A, 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 <=), ") &
(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;

View file

@ -0,0 +1,16 @@
text s, t;
s = "occidental";
t = "oriental";
# operator case sensitive comparison
o_form("~ vs ~ (==, !=, <, <=, >=, >): ~ ~ ~ ~ ~ ~\n", s, t, s == t, s != t, s < t, s <= t, s >= t, s > t);
s = "Oriental";
t = "oriental";
# case sensitive comparison
o_form("~ vs ~ (==, !=, <, >): ~ ~ ~ ~\n", s, t, !compare(s, t), compare(s, t), compare(s, t) < 0, 0 < compare(s, t));
# case insensitive comparison
o_form("~ vs ~ (==, !=, <, >): ~ ~ ~ ~\n", s, t, !icompare(s, t), icompare(s, t), icompare(s, t) < 0, 0 < icompare(s, t));

View file

@ -0,0 +1,35 @@
public class Compare
{
/**
* Test in the developer console:
* Compare.compare('Hello', 'Hello');
* Compare.compare('5', '5.0');
* Compare.compare('java', 'Java');
* Compare.compare('ĴÃVÁ', 'ĴÃVÁ');
*/
public static void compare (String A, String B)
{
if (A.equals(B))
System.debug(A + ' and ' + B + ' are lexically equal.');
else
System.debug(A + ' and ' + B + ' are not lexically equal.');
if (A.equalsIgnoreCase(B))
System.debug(A + ' and ' + B + ' are case-insensitive lexically equal.');
else
System.debug(A + ' and ' + B + ' are not case-insensitive lexically equal.');
if (A.compareTo(B) < 0)
System.debug(A + ' is lexically before ' + B);
else if (A.compareTo(B) > 0)
System.debug(A + ' is lexically after ' + B);
if (A.compareTo(B) >= 0)
System.debug(A + ' is not lexically before ' + B);
if (A.compareTo(B) <= 0)
System.debug(A + ' is not lexically after ' + B);
System.debug('The lexical relationship is: ' + A.compareTo(B));
}
}

View file

@ -0,0 +1,52 @@
--Comparing two strings for exact equality
set s1 to "this"
set s2 to "that"
if s1 is s2 then
-- strings are equal
end if
--Comparing two strings for inequality (i.e., the inverse of exact equality)
if s1 is not s2 then
-- string are not equal
end if
-- Comparing two strings to see if one is lexically ordered before than the other
if s1 < s2 then
-- s1 is lexically ordered before s2
end if
-- Comparing two strings to see if one is lexically ordered after than the other
if s1 > s2 then
-- s1 is lexically ordered after s2
end if
-- How to achieve both case sensitive comparisons and case insensitive comparisons within the language
set s1 to "this"
set s2 to "This"
considering case
if s1 is s2 then
-- strings are equal with case considering
end if
end considering
ignoring case -- default
if s2 is s2 then
-- string are equal without case considering
end if
end ignoring
-- Demonstrate any other kinds of string comparisons that the language provides, particularly as it relates to your type system. For example, you might demonstrate the difference between generic/polymorphic comparison and coercive/allomorphic comparison if your language supports such a distinction.
-- When comparing the right object is coerced into the same type as the object left from the operator. This implicit coercion enables to compare integers with strings (containining integer values).
set s1 to "3"
set int1 to 2
if s1 < int1 then
-- comparison is lexically
end if
if int1 < s1 then
-- comparison is numeric
end if

View file

@ -0,0 +1,10 @@
loop [["YUP" "YUP"] ["YUP" "Yup"] ["bot" "bat"] ["aaa" "zz"]] 'x [
print [x\0 "=" x\1 "=>" x\0 = x\1]
print [x\0 "=" x\1 "(case-insensitive) =>" (upper x\0) = upper x\1]
print [x\0 "<>" x\1 "=>" x\0 <> x\1]
print [x\0 ">" x\1 "=>" x\0 > x\1]
print [x\0 ">=" x\1 "=>" x\0 >= x\1]
print [x\0 "<" x\1 "=>" x\0 < x\1]
print [x\0 "=<" x\1 "=>" x\0 =< x\1]
print "----"
]

View file

@ -0,0 +1,16 @@
fun compare(a, b):
print("\n$a is of type ${typeof(a)} and $b is of type ${typeof(b)}")
if a < b: print("$a is strictly less than $b")
if a <= b: print("$a is less than or equal to $b")
if a > b: print("$a is strictly greater than $b")
if a >= b: print("$a is greater than or equal to $b")
if a == b: print("$a is equal to $b")
if a != b: print("$a is not equal to $b")
if a is b: print("$a has object identity with $b")
if a is not b: print("$a has negated object identity with $b")
compare("YUP", "YUP")
compare('a', 'z')
compare("24", "123")
compare(24, 123)
compare(5.0, 5)

View file

@ -0,0 +1,18 @@
exact_equality(a,b){
return (a==b)
}
exact_inequality(a,b){
return !(a==b)
}
equality(a,b){
return (a=b)
}
inequality(a,b){
return !(a=b)
}
ordered_before(a,b){
return ("" a < "" b)
}
ordered_after(a,b){
return ("" a > "" b)
}

View file

@ -0,0 +1,8 @@
for a, b in {"alpha":"beta", "Gamma":"gamma", 100:5}
MsgBox % a " vs " b "`n"
. "exact_equality case sensitive : " exact_equality(a,b) "`n"
. "exact_inequality case sensitive :" exact_inequality(a,b) "`n"
. "equality case insensitive : " equality(a,b) "`n"
. "inequality case insensitive : " inequality(a,b) "`n"
. "ordered_before : " ordered_before(a,b) "`n"
. "ordered_after : " ordered_after(a,b) "`n"

View file

@ -0,0 +1,16 @@
Method "string comparisons_,_" is
[
a : string,
b : string
|
Print: "a & b are equal? " ++ “a = b”;
Print: "a & b are not equal? " ++ “a ≠ b”;
// Inequalities compare by code point
Print: "a is lexically before b? " ++ “a < b”;
Print: "a is lexically after b? " ++ “a > b”;
// Supports non-strict inequalities
Print: "a is not lexically before b? " ++ “a ≥ b”;
Print: "a is not lexically after b? " ++ “a ≤ b”;
// Case-insensitive comparison requires a manual case conversion
Print: "a & b are equal case-insensitively?" ++ “lowercase a = lowercase b”;
];

View file

@ -0,0 +1,9 @@
10 LET "A$="BELL"
20 LET B$="BELT"
30 IF A$ = B$ THEN PRINT "THE STRINGS ARE EQUAL": REM TEST FOR EQUALITY
40 IF A$ <> B$ THEN PRINT "THE STRINGS ARE NOT EQUAL": REM TEST FOR INEQUALITY
50 IF A$ > B$ THEN PRINT A$;" IS LEXICALLY HIGHER THAN ";B$: REM TEST FOR LEXICALLY HIGHER
60 IF A$ < B$ THEN PRINT A$;" IS LEXICALLY LOWER THAN ";B$: REM TEST FOR LEXICALLY LOWER
70 IF A$ <= B$ THEN PRINT A$;" IS NOT LEXICALLY HIGHER THAN ";B$
80 IF A$ >= B$ THEN PRINT A$;" IS NOT LEXICALLY LOWER THAN ";B$
90 END

View file

@ -0,0 +1,3 @@
10 LET A$="BELT"
20 LET B$="belt"
30 IF UPPER$(A$)=UPPER$(B$) THEN PRINT "Disregarding lettercase, the strings are the same."

View file

@ -0,0 +1,26 @@
function StringCompare(s1, s2, ignoreCase)
if ignoreCase then
s = lower(s1)
t = lower(s2)
else
s = s1
t = s2
end if
if s < t then return " comes before "
if s = t then return " is equal to "
return " comes after "
end function
s1 = "Dog" : s2 = "Dog"
print s1; StringCompare(s1, s2, False); s2
s2 = "Cat"
print s1; StringCompare(s1, s2, False); s2
s2 = "Rat"
print s1; StringCompare(s1, s2, False); s2
s2 = "dog"
print s1; StringCompare(s1, s2, False); s2
print s1; StringCompare(s1, s2, True); s2; " if case is ignored"
s1 = "Dog" : s2 = "Pig"
s3 = StringCompare(s1, s2, False)
if s3 <> " is equal to " then print s1; " is not equal to "; s2
end

View file

@ -0,0 +1,29 @@
REM >strcomp
shav$ = "Shaw, George Bernard"
shakes$ = "Shakespeare, William"
:
REM test equality
IF shav$ = shakes$ THEN PRINT "The two strings are equal" ELSE PRINT "The two strings are not equal"
:
REM test inequality
IF shav$ <> shakes$ THEN PRINT "The two strings are unequal" ELSE PRINT "The two strings are not unequal"
:
REM test lexical ordering
IF shav$ > shakes$ THEN PRINT shav$; " is lexically higher than "; shakes$ ELSE PRINT shav$; " is not lexically higher than "; shakes$
IF shav$ < shakes$ THEN PRINT shav$; " is lexically lower than "; shakes$ ELSE PRINT shav$; " is not lexically lower than "; shakes$
REM the >= and <= operators can also be used, & behave as expected
:
REM string comparison is case-sensitive by default, and BBC BASIC
REM does not provide built-in functions to convert to all upper
REM or all lower case; but it is easy enough to define one
:
IF FN_upper(shav$) = FN_upper(shakes$) THEN PRINT "The two strings are equal (disregarding case)" ELSE PRINT "The two strings are not equal (even disregarding case)"
END
:
DEF FN_upper(s$)
LOCAL i%, ns$
ns$ = ""
FOR i% = 1 TO LEN s$
IF ASC(MID$(s$, i%, 1)) >= ASC "a" AND ASC(MID$(s$, i%, 1)) <= ASC "z" THEN ns$ += CHR$(ASC(MID$(s$, i%, 1)) - &20) ELSE ns$ += MID$(s$, i%, 1)
NEXT
= ns$

View file

@ -0,0 +1,102 @@
( {Comparing two strings for exact equality}
& ( ( @(abc:abc)
& @(123:%123)
{Previous pairs of strings are exactly equal}
)
& ( @(abc:Abc)
| @(123:%246/2)
| @(abc:ab)
| @(123:%12)
| {Previous pairs of strings are not exactly equal}
)
)
{Comparing two strings for inequality (i.e., the inverse of exact equality)}
& ( ( @(abc:~<>abc)
& @(abc:~<>Abc)
{Previous pairs of strings are more or less equal}
)
& ( @(abc:~<>ab)
| {Previous pairs of strings are not more or less equal}
)
)
{Comparing two strings to see if one is lexically ordered before than the other}
& ( ( @(Abc:<abc)
& @(Abc:<a)
& @(123:<%246/2)
& @(123:<%2)
& @(12:<%123)
& @(ab:<abc)
{Previous pairs of strings are lexically ordered one before the other}
)
& ( @(abc:<abc)
| @(abc:<Abc)
| @(246/2:<%123)
| @(abc:<ab)
| @(123:<%12)
| @(123:<%123)
| {Previous pairs of strings are not lexically ordered one before the other}
)
)
{Comparing two strings to see if one is lexically ordered after than the other}
& ( ( @(abc:>Abc)
& @(a:>Abc)
& @(246/2:>%123)
& @(2:>%123)
& @(123:>%12)
& @(abc:>ab)
{Previous pairs of strings are lexically ordered one after the other}
)
& ( @(abc:>abc)
| @(Abc:>abc)
| @(123:>%246/2)
| @(ab:>abc)
| @(12:>%123)
| @(123:>%123)
| {Previous pairs of strings are not lexically ordered one after the other}
)
)
{How to achieve both case sensitive comparisons and case insensitive comparisons within
the language}
& ( ( @(abc:~<>abc)
& @(abc:~<>Abc)
& @(БЪЛГАРСКИ:~<>български)
{Previous pairs of strings are more or less equal}
)
& ( @(abc:~<>ab)
| {Previous pairs of strings are not more or less equal}
)
)
{How the language handles comparison of numeric strings if these are not treated lexically}
& ( ( @(246/2:123)
& @(2:<123)
& @(123:>12)
& @(123:246/2)
& @(12:<123)
{Previous numeric string comparisons succeed}
)
& ( @(123:<246/2)
| @(12:>123)
| @(123:>123)
| @(123:~123)
| {Previous numeric string comparisons fail}
)
)
{Demonstrate any other kinds of string comparisons that the language provides, particularly
as it relates to your type system. For example, you might demonstrate the difference between
generic/polymorphic comparison and coercive/allomorphic comparison if your language supports
such a distinction.}
& ( ( @(246/2:>12--3)
& @(2:>123kg)
& @(123:<12d)
& @(123:~24/6/2)
& @(12a:>123)
{Previous coercive string comparisons succeed}
)
& ( @(2013-05-01:20130501)
| @(246/2a:123a)
| @(1239:<123-)
| {Previous coercive string comparisons fail}
)
)
& done
);

View file

@ -0,0 +1,8 @@
blsq ) "abc""abc"==
1
blsq ) "abc""abc"!=
0
blsq ) "abc""Abc"cm
1
blsq ) "ABC""Abc"cm
-1

View file

@ -0,0 +1,39 @@
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
template <typename T>
void demo_compare(const T &a, const T &b, const std::string &semantically) {
std::cout << a << " and " << b << " are " << ((a == b) ? "" : "not ")
<< "exactly " << semantically << " equal." << std::endl;
std::cout << a << " and " << b << " are " << ((a != b) ? "" : "not ")
<< semantically << "inequal." << std::endl;
std::cout << a << " is " << ((a < b) ? "" : "not ") << semantically
<< " ordered before " << b << '.' << std::endl;
std::cout << a << " is " << ((a > b) ? "" : "not ") << semantically
<< " ordered after " << b << '.' << std::endl;
}
int main(int argc, char *argv[]) {
// Case-sensitive comparisons.
std::string a((argc > 1) ? argv[1] : "1.2.Foo");
std::string b((argc > 2) ? argv[2] : "1.3.Bar");
demo_compare<std::string>(a, b, "lexically");
// Case-insensitive comparisons by folding both strings to a common case.
std::transform(a.begin(), a.end(), a.begin(), ::tolower);
std::transform(b.begin(), b.end(), b.begin(), ::tolower);
demo_compare<std::string>(a, b, "lexically");
// Numeric comparisons; here 'double' could be any type for which the
// relevant >> operator is defined, eg int, long, etc.
double numA, numB;
std::istringstream(a) >> numA;
std::istringstream(b) >> numB;
demo_compare<double>(numA, numB, "numerically");
return (a == b);
}

View file

@ -0,0 +1,2 @@
/* WRONG! */
if (strcmp(a,b)) action_on_equality();

View file

@ -0,0 +1,61 @@
/*
compilation and test in bash
$ a=./c && make $a && $a ball bell ball ball YUP YEP ball BELL ball BALL YUP yep
cc -Wall -c -o c.o c.c
eq , ne , gt , lt , ge , le
ball 0 1 0 1 0 1 bell
ball 0 1 0 1 0 1 bell ignoring case
ball 1 0 0 0 1 1 ball
ball 1 0 0 0 1 1 ball ignoring case
YUP 0 1 1 0 1 0 YEP
YUP 0 1 1 0 1 0 YEP ignoring case
ball 0 1 1 0 1 0 BELL
ball 0 1 0 1 0 1 BELL ignoring case
ball 0 1 1 0 1 0 BALL
ball 1 0 0 0 1 1 BALL ignoring case
YUP 0 1 0 1 0 1 yep
YUP 0 1 1 0 1 0 yep ignoring case
*/
#include<string.h>
#define STREQ(A,B) (0==strcmp((A),(B)))
#define STRNE(A,B) (!STREQ(A,B))
#define STRLT(A,B) (strcmp((A),(B))<0)
#define STRLE(A,B) (strcmp((A),(B))<=0)
#define STRGT(A,B) STRLT(B,A)
#define STRGE(A,B) STRLE(B,A)
#define STRCEQ(A,B) (0==strcasecmp((A),(B)))
#define STRCNE(A,B) (!STRCEQ(A,B))
#define STRCLT(A,B) (strcasecmp((A),(B))<0)
#define STRCLE(A,B) (strcasecmp((A),(B))<=0)
#define STRCGT(A,B) STRCLT(B,A)
#define STRCGE(A,B) STRCLE(B,A)
#include<stdio.h>
void compare(const char*a, const char*b) {
printf("%s%2d%2d%2d%2d%2d%2d %s\n",
a,
STREQ(a,b), STRNE(a,b), STRGT(a,b), STRLT(a,b), STRGE(a,b), STRLE(a,b),
b
);
}
void comparecase(const char*a, const char*b) {
printf("%s%2d%2d%2d%2d%2d%2d %s ignoring case\n",
a,
STRCEQ(a,b), STRCNE(a,b), STRCGT(a,b), STRCLT(a,b), STRCGE(a,b), STRCLE(a,b),
b
);
}
int main(int ac, char*av[]) {
char*a,*b;
puts("\teq , ne , gt , lt , ge , le");
while (0 < (ac -= 2)) {
a = *++av, b = *++av;
compare(a, b);
comparecase(a, b);
}
return 0;
}

View file

@ -0,0 +1,3 @@
"hello" = "hello" *> equality
"helloo" <> "hello" *> inequality
"aello" < "hello" *> lexical ordering

View file

@ -0,0 +1,3 @@
FUNCTION STANDARD-COMPARE("hello", "hello") *> "="
FUNCTION STANDARD-COMPARE("aello", "hello") *> "<"
FUNCTION STANDARD-COMPARE("hello", "aello") *> ">"

View file

@ -0,0 +1,2 @@
"hello " = "hello" *> True
X"00" > X"0000" *> True

View file

@ -0,0 +1,12 @@
IF s1 == s2
? "The strings are equal"
ENDIF
IF .NOT. (s1 == s2)
? "The strings are not equal"
ENDIF
IF s1 > s2
? "s2 is lexically ordered before than s1"
ENDIF
IF s1 < s2
? "s2 is lexically ordered after than s1"
ENDIF

View file

@ -0,0 +1,3 @@
IF Upper(s1) == Upper(s2)
? "The strings are equal"
ENDIF

View file

@ -0,0 +1,5 @@
(= "abc" "def") ; false
(= "abc" "abc") ; true
(not= "abc" "def") ; true
(not= "abc" "abc") ; false

View file

@ -0,0 +1,2 @@
(= "abc" "abc" "abc" "abc") ; true
(= "abc" "abc" "abc" "def") ; false

View file

@ -0,0 +1 @@
(apply = ["abc" "abc" "abc" "abc"]) ; true

View file

@ -0,0 +1,14 @@
(defn str-before [a b]
(neg? (compare a b)))
(defn str-after [a b]
(pos? (compare a b)))
(str-before "abc" "def") ; true
(str-before "def" "abc") ; false
(str-before "abc" "abc") ; false
(str-after "abc" "def") ; false
(str-after "def" "abc") ; false
(sort ["foo" "bar" "baz"]) ; ("bar" "baz" "foo")

View file

@ -0,0 +1,5 @@
(defn str-caseless= [a b]
(= (clojure.string/lower-case a)
(clojure.string/lower-case b)))
(str-caseless= "foo" "fOO") ; true

View file

@ -0,0 +1,12 @@
(defn str-fuzzy= [a b]
(let [cook (fn [v] (clojure.string/trim (str v)))]
(= (cook a) (cook b))))
(str-fuzzy= "abc" " abc") ; true
(str-fuzzy= "abc" "abc ") ; true
(str-fuzzy= "abc" " abc ") ; true
(str-fuzzy= " 42 " 42) ; true
(str-fuzzy= " 42 " (* 6 7)) ; true
(str-fuzzy= " 2.5" (/ 5.0 2)) ; true

View file

@ -0,0 +1,8 @@
(def s1 (str "abc" "def"))
(def s2 (str "ab" "cdef"))
(= s1 "abcdef") ; true
(= s1 s2) ; true
(identical? s1 "abcdef") ; false
(identical? s1 s2) ; false

View file

@ -0,0 +1,8 @@
(defn istr [s]
(.intern s))
(def s3 (istr s1))
(def s4 (istr s2))
(= s3 s4) ; true
(identical? s3 s4) ; true

View file

@ -0,0 +1,24 @@
<cffunction name="CompareString">
<cfargument name="String1" type="string">
<cfargument name="String2" type="string">
<cfset VARIABLES.Result = "" >
<cfif ARGUMENTS.String1 LT ARGUMENTS.String2 >
<cfset VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is less than '" & ARGUMENTS.String2 & "')" >
</cfif>
<cfif ARGUMENTS.String1 LTE ARGUMENTS.String2 >
<cfset VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is less than or equal to '" & ARGUMENTS.String2 & "')" >
</cfif>
<cfif ARGUMENTS.String1 GT ARGUMENTS.String2 >
<cfset VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is greater than '" & ARGUMENTS.String2 & "')" >
</cfif>
<cfif ARGUMENTS.String1 GTE ARGUMENTS.String2 >
<cfset VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is greater than or equal to '" & ARGUMENTS.String2 & "')" >
</cfif>
<cfif ARGUMENTS.String1 EQ ARGUMENTS.String2 >
<cfset VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is equal to '" & ARGUMENTS.String2 & "')" >
</cfif>
<cfif ARGUMENTS.String1 NEQ ARGUMENTS.String2 >
<cfset VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is not equal to '" & ARGUMENTS.String2 & "')" >
</cfif>
<cfreturn VARIABLES.Result >
</cffunction>

View file

@ -0,0 +1,24 @@
<cfscript>
function CompareString( String1, String2 ) {
VARIABLES.Result = "";
if ( ARGUMENTS.String1 LT ARGUMENTS.String2 ) {
VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is less than '" & ARGUMENTS.String2 & "')";
}
if ( ARGUMENTS.String1 LTE ARGUMENTS.String2 ) {
VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is less than or equal to '" & ARGUMENTS.String2 & "')";
}
if ( ARGUMENTS.String1 GT ARGUMENTS.String2 ) {
VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is greater than '" & ARGUMENTS.String2 & "')";
}
if ( ARGUMENTS.String1 GTE ARGUMENTS.String2 ) {
VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is greater than or equal to '" & ARGUMENTS.String2 & "')";
}
if ( ARGUMENTS.String1 EQ ARGUMENTS.String2 ) {
VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is equal to '" & ARGUMENTS.String2 & "')";
}
if ( ARGUMENTS.String1 NEQ ARGUMENTS.String2 ) {
VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is not equal to '" & ARGUMENTS.String2 & "')";
}
return VARIABLES.Result;
}
</cfscript>

View file

@ -0,0 +1,18 @@
>(string= "foo" "foo")
T
> (string= "foo" "FOO")
NIL
> (string/= "foo" "bar")
0
> (string/= "bar" "baz")
2
> (string/= "foo" "foo")
NIL
> (string> "foo" "Foo")
0
> (string< "foo" "Foo")
NIL
> (string>= "FOo" "Foo")
NIL
> (string<= "FOo" "Foo")
1

View file

@ -0,0 +1,12 @@
> (string-equal "foo" "FOo")
T
> (string-not-equal "foo" "FOO")
NIL
> (string-greaterp "foo" "Foo")
NIL
> (string-lessp "BAR" "foo")
0
> (string-not-greaterp "foo" "Foo")
3
> (string-not-lessp "baz" "bAr")
2

View file

@ -0,0 +1,4 @@
> (string> "45" "12345")
0
> (string> "45" "9")
NIL

View file

@ -0,0 +1,31 @@
MODULE StringComparision;
IMPORT StdLog,Strings;
PROCEDURE Do*;
VAR
str1,str2,aux1,aux2: ARRAY 128 OF CHAR;
BEGIN
str1 := "abcde";str2 := "abcde";
StdLog.String(str1+" equals " + str2 + ":> ");StdLog.Bool(str1 = str2);StdLog.Ln;
str2 := "abcd";
StdLog.String(str1+" equals " + str2 + ":> ");StdLog.Bool(str1 = str2);StdLog.Ln;
StdLog.String(str1+" greater than " + str2 + ":> ");StdLog.Bool(str1 > str2);StdLog.Ln;
StdLog.String(str1+" lower than " + str2 + ":> ");StdLog.Bool(str1 < str2);StdLog.Ln;
str2 := "ABCDE";
StdLog.String(str1+" equals " + str2 + ":> ");StdLog.Bool(str1 = str2);StdLog.Ln;
StdLog.String(str1+" greater than " + str2 + ":> ");StdLog.Bool(str1 > str2);StdLog.Ln;
StdLog.String(str1+" lower than " + str2 + ":> ");StdLog.Bool(str1 < str2);StdLog.Ln;
Strings.ToLower(str1,aux1);Strings.ToLower(str2,aux2);
StdLog.String(str1+" equals (case insensitive) " + str2 + ":> ");StdLog.Bool(aux1 = aux2);StdLog.Ln;
str1 := "01234";str2 := "01234";
StdLog.String(str1+" equals " + str2 + ":> ");StdLog.Bool(str1 = str2);StdLog.Ln;
str2 := "0123";
StdLog.String(str1+" equals " + str2 + ":> ");StdLog.Bool(str1 = str2);StdLog.Ln;
StdLog.String(str1+" greater than " + str2 + ":> ");StdLog.Bool(str1 > str2);StdLog.Ln;
StdLog.String(str1+" lower than " + str2 + ":> ");StdLog.Bool(str1 < str2);StdLog.Ln;
END Do;
END StringComparision.

View file

@ -0,0 +1,23 @@
import std.stdio, std.string, std.algorithm;
void main() {
auto s = "abcd";
/* Comparing two strings for exact equality */
assert (s == "abcd"); // same object
/* Comparing two strings for inequality */
assert(s != "ABCD"); // different objects
/* Comparing the lexical order of two strings;
-1 means smaller, 0 means equal, 1 means larger */
assert(s.icmp("Bcde") == -1); // case insensitive
assert(s.cmp("Bcde") == 1); // case sensitive
assert(s.icmp("Aabc") == 1); // case insensitive
assert(s.cmp("Aabc") == 1); // case sensitive
assert(s.icmp("ABCD") == 0); // case insensitive
assert(s.cmp("ABCD") == 1); // case sensitive
}

View file

@ -0,0 +1,22 @@
procedure ShowCompares(Memo: TMemo; S1,S2: string);
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 ShowStringCompares(Memo: TMemo);
begin
ShowCompares(Memo,'Equal', 'Equal');
ShowCompares(Memo,'Case', 'CASE');
ShowCompares(Memo,'91', '1234');
ShowCompares(Memo,'boy', 'cowboy');
end;

View file

@ -0,0 +1,23 @@
func compare(a, b) {
if a == b {
print("'\(a)' and '\(b)' are lexically equal.")
}
if a != b {
print("'\(a)' and '\(b)' are not lexically equal.")
}
if a < b {
print("'\(a)' is lexically before '\(b)'.")
}
if a > b {
print("'\(a)' is lexically after '\(b)'.")
}
if a >= b {
print("'\(a)' is not lexically before '\(b)'.")
}
if a <= b {
print("'\(a)' is not lexically after '\(b)'.")
}
}
compare("cat", "dog")

View file

@ -0,0 +1,9 @@
stringA$ = "String"
stringB$ = "string"
stringC$ = "string"
if stringB$ = stringC$
print "\"" & stringB$ & "\" is equal to \"" & stringC$ & "\""
.
if stringA$ <> stringB$
print "\"" & stringA$ & "\" is not equal to \"" & stringB$ & "\""
.

View file

@ -0,0 +1,20 @@
import extensions;
compareStrings = (val1,val2)
{
if (val1 == val2) { console.printLine("The strings ",val1," and ",val2," are equal") };
if (val1 != val2) { console.printLine("The strings ",val1," and ",val2," are not equal") };
if (val1 > val2) { console.printLine("The string ",val1," is lexically after than ",val2) };
if (val1 < val2) { console.printLine("The string ",val1," is lexically before than ",val2) };
if (val1 >= val2) { console.printLine("The string ",val1," is not lexically before than ",val2) };
if (val1 <= val2) { console.printLine("The string ",val1," is not lexically after than ",val2) }
};
public program()
{
var s1 := "this";
var s2 := "that";
compareStrings(s1,s2);
console.readChar()
}

View file

@ -0,0 +1,9 @@
s = "abcd"
s == "abcd" #=> true
s == "abce" #=> false
s != "abcd" #=> false
s != "abce" #=> true
s > "abcd" #=> false
s < "abce" #=> true
s >= "abce" #=> false
s <= "abce" #=> true

View file

@ -0,0 +1,12 @@
10> V = "abcd".
"abcd"
11> V =:= "abcd".
true
12> V =/= "abcd".
false
13> V < "b".
true
15> V > "aa".
true
16> string:to_lower(V) =:= string:to_lower("ABCD").
true

View file

@ -0,0 +1,33 @@
open System
// self defined operators for case insensitive comparison
let (<~) a b = String.Compare(a, b, StringComparison.OrdinalIgnoreCase) < 0
let (<=~) a b = String.Compare(a, b, StringComparison.OrdinalIgnoreCase) <= 0
let (>~) a b = String.Compare(a, b, StringComparison.OrdinalIgnoreCase) > 0
let (>=~) a b = String.Compare(a, b, StringComparison.OrdinalIgnoreCase) >= 0
let (=~) a b = String.Compare(a, b, StringComparison.OrdinalIgnoreCase) = 0
let (<>~) a b = String.Compare(a, b, StringComparison.OrdinalIgnoreCase) <> 0
let compare a b = // standard operators:
if a < b then printfn "%s is strictly less than %s" a b
if a <= b then printfn "%s is less than or equal to %s" a b
if a > b then printfn "%s is strictly greater than %s" a b
if a >= b then printfn "%s is greater than or equal to %s" a b
if a = b then printfn "%s is equal to %s" a b
if a <> b then printfn "%s is not equal to %s" a b
// and our case insensitive self defined operators:
if a <~ b then printfn "%s is strictly less than %s (case insensitive)" a b
if a <=~ b then printfn "%s is less than or equal to %s (case insensitive)" a b
if a >~ b then printfn "%s is strictly greater than %s (case insensitive)" a b
if a >=~ b then printfn "%s is greater than or equal to %s (case insensitive)" a b
if a =~ b then printfn "%s is equal to %s (case insensitive)" a b
if a <>~ b then printfn "%s is not equal to %s (case insensitive)" a b
[<EntryPoint>]
let main argv =
compare "YUP" "YUP"
compare "BALL" "BELL"
compare "24" "123"
compare "BELL" "bELL"
0

View file

@ -0,0 +1,18 @@
USING: ascii math.order sorting.human ;
IN: scratchpad "foo" "bar" = . ! compare for equality
f
IN: scratchpad "foo" "bar" = not . ! compare for inequality
t
IN: scratchpad "foo" "bar" before? . ! lexically ordered before?
f
IN: scratchpad "foo" "bar" after? . ! lexically ordered after?
t
IN: scratchpad "Foo" "foo" <=> . ! case-sensitive comparison
+lt+
IN: scratchpad "Foo" "foo" [ >lower ] bi@ <=> . ! case-insensitive comparison
+eq+
IN: scratchpad "a1" "a03" <=> . ! comparing numeric strings
+gt+
IN: scratchpad "a1" "a03" human<=> . ! comparing numeric strings like a human
+lt+

View file

@ -0,0 +1,35 @@
/* created by Aykayayciti Earl Lamont Montgomery
April 9th, 2018 */
e = "early"
l = "toast"
g = "cheese"
b = "cheese"
e2 = "early"
num1 = 123
num2 = 456
> e == e2 ? @ "$e equals $e2" : @ "$e does not equal $e2"
> e != e2 ? @ "$e does not equal $e2": @ "$e equals $e2"
// produces -1 for less than
> b.cmpi(l) == 1 ? @ "$b is grater than $l" : @ "$l is grater than $b"
// produces 1 for greater than
> l.cmpi(b) == 1 ? @ "$l is grater than $b" : @ "$b is grater than $l"
// produces 0 for equal (but could be greater than or equal)
> b.cmpi(g) == 1 or b.cmpi(g) == 0 ? @ "$b is grater than or equal to $g" : @ "$b is not >= $g"
// produces 0 for equal (but could be less than or equal)
>b.cmpi(g) == -1 or b.cmpi(g) == 0 ? @ "$b is less than or equal to $g" : @ "$b is not <= $g"
function NumCompare(num1, num2)
if num1 < num2
ans = " < "
elif num1 > num2
ans = " > "
else
ans = " = "
end
return ans
end
result = NumCompare(num1, num2)
> @ "$num1 $result $num2"

View file

@ -0,0 +1,6 @@
: str-eq ( str len str len -- ? ) compare 0= ;
: str-neq ( str len str len -- ? ) compare 0<> ;
: str-lt ( str len str len -- ? ) compare 0< ;
: str-gt ( str len str len -- ? ) compare 0> ;
: str-le ( str len str len -- ? ) compare 0<= ;
: str-ge ( str len str len -- ? ) compare 0>= ;

View file

@ -0,0 +1,2 @@
PRINT 42,N
42 FORMAT (14HThe answer is ,I9)

View file

@ -0,0 +1,40 @@
' FB 1.05.0
' Strings in FB natively support the relational operators which compare lexically on a case-sensitive basis.
' There are no special provisions for numerical strings.
' There are no other types of string comparison for the built-in types though 'user defined types'
' can specify their own comparisons by over-loading the relational operators.
Function StringCompare(s1 As Const String, s2 As Const String, ignoreCase As Boolean = false) As String
Dim As String s, t ' need new string variables as the strings passed in can't be changed
If ignoreCase Then
s = LCase(s1)
t = LCase(s2)
Else
s = s1
t = s2
End If
If s < t Then Return " comes before "
If s = t Then Return " is equal to "
Return " comes after "
End Function
Dim As Integer result
Dim As String s1, s2, s3
s1 = "Dog" : s2 = "Dog"
Print s1; StringCompare(s1, s2); s2
s2 = "Cat"
Print s1; StringCompare(s1, s2); s2
s2 = "Rat"
Print s1; StringCompare(s1, s2); s2
s2 = "dog"
Print s1; StringCompare(s1, s2); s2
Print s1; StringCompare(s1, s2, True); s2; " if case is ignored"
s1 = "Dog" : s2 = "Pig"
s3 = StringCompare(s1, s2)
If s3 <> " is equal to " Then
Print s1; " is not equal to "; s2
End If
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,74 @@
void local fn StringComparison
CFStringRef s1, s2
NSComparisonResult result
window 1, @"String Comparison"
print @"• equal - case sensitive •"
s1 = @"alpha" : s2 = @"alpha"
if ( fn StringIsEqual( s1, s2 ) )
printf @"\"%@\" is equal to \"%@\"",s1,s2
end if
result = fn StringCompare( s1, s2 )
if ( result == NSOrderedSame )
printf @"\"%@\" is equal to \"%@\"",s1,s2
end if
select ( s1 )
case s2
printf @"\"%@\" is equal to \"%@\"",s1,s2
case else
printf @"\"%@\" is not equal to \"%@\"",s1,s2
end select
print @"\n• not equal - case sensitive •"
s2 = @"bravo"
if ( fn StringIsEqual( s1, s2 ) == NO )
printf @"\"%@\" is not equal to \"%@\"",s1,s2
end if
result = fn StringCompare( s1, s2 )
if ( result != NSOrderedSame )
printf @"\"%@\" is not equal to \"%@\"",s1,s2
end if
select ( s1 )
case s2
printf @"\"%@\" is equal to \"%@\"",s1,s2
case else
printf @"\"%@\" is not equal to \"%@\"",s1,s2
end select
print @"\n• ordered before - case sensitive •"
result = fn StringCompare( s1, s2 )
if ( result == NSOrderedAscending )
printf @"\"%@\" is ordered before \"%@\"",s1,s2
end if
print @"\n• ordered after - case sensitive •"
result = fn StringCompare( s2, s1 )
if ( result == NSOrderedDescending )
printf @"\"%@\" is ordered after \"%@\"",s2,s1
end if
print @"\n• equal - case insensitive •"
s2 = @"AlPhA"
result = fn StringCaseInsensitiveCompare( s1, s2 )
if ( result == NSOrderedSame )
printf @"\"%@\" is equal to \"%@\"",s1,s2
end if
result = fn StringCompareWithOptions( s1, s2, NSCaseInsensitiveSearch )
if ( result == NSOrderedSame )
printf @"\"%@\" is equal to \"%@\"",s1,s2
end if
if ( fn StringIsEqual( lcase(s1), lcase(s2) ) )
printf @"\"%@\" is equal to \"%@\"",s1,s2
end if
end fn
fn StringComparison
HandleEvents

View file

@ -0,0 +1,50 @@
package main
import (
"fmt"
"strings"
)
func main() {
// Go language string comparison operators:
c := "cat"
d := "dog"
if c == d {
fmt.Println(c, "is bytewise identical to", d)
}
if c != d {
fmt.Println(c, "is bytewise different from", d)
}
if c > d {
fmt.Println(c, "is lexically bytewise greater than", d)
}
if c < d {
fmt.Println(c, "is lexically bytewise less than", d)
}
if c >= d {
fmt.Println(c, "is lexically bytewise greater than or equal to", d)
}
if c <= d {
fmt.Println(c, "is lexically bytewise less than or equal to", d)
}
// Go is strongly typed and will not directly compare a value of string
// type to a value of numeric type.
// A case insensitive compare can be done with a function in the strings
// package in the Go standard library:
eqf := `when interpreted as UTF-8 and compared under Unicode
simple case folding rules.`
if strings.EqualFold(c, d) {
fmt.Println(c, "equal to", d, eqf)
} else {
fmt.Println(c, "not equal to", d, eqf)
}
// Seeing that the built in operators work bytewise and the library
// case folding functions interpret UTF-8, you might then ask about
// other equality and inequality tests that interpret UTF-8.
// Functions for this are not in the Go standard library but are in
// the Go "sub repository" at golang.org/x/text. There is support
// for Unicode normalization, collation tables, and locale sensitive
// comparisons.
}

View file

@ -0,0 +1,12 @@
IF s1 == s2
? "The strings are equal"
ENDIF
IF !( s1 == s2 )
? "The strings are not equal"
ENDIF
IF s1 > s2
? "s2 is lexically ordered before than s1"
ENDIF
IF s1 < s2
? "s2 is lexically ordered after than s1"
ENDIF

View file

@ -0,0 +1,3 @@
IF Upper( s1 ) == Upper( s2 )
? "The strings are equal"
ENDIF

View file

@ -0,0 +1,15 @@
> "abc" == "abc"
True
> "abc" /= "abc"
False
> "abc" <= "abcd"
True
> "abc" <= "abC"
False
> "HELLOWORLD" == "HelloWorld"
False
> :m +Data.Char
> map toLower "ABC"
"abc"
> map toLower "HELLOWORLD" == map toLower "HelloWorld"
True

View file

@ -0,0 +1,14 @@
procedure main(A)
s1 := A[1] | "a"
s2 := A[2] | "b"
# These first four are case-sensitive
s1 == s2 # Are they equal?
s1 ~== s2 # Are they unequal?
s1 << s2 # Does s1 come before s2?
s1 >> s2 # Does s1 come after s2?
map(s1) == map(s2) # Caseless comparison
"123" >> "12" # Lexical comparison
"123" > "12" # Numeric comparison
"123" >> 12 # Lexical comparison (12 coerced into "12")
"123" > 12 # Numeric comparison ("123" coerced into 123)
end

View file

@ -0,0 +1,6 @@
eq=: -: NB. equal
ne=: -.@-: NB. not equal
gt=: {.@/:@,&boxopen *. ne NB. lexically greater than
lt=: -.@{.@/:@,&boxopen *. ne NB. lexically less than
ge=: {.@/:@,&boxopen +. eq NB. lexically greater than or equal to
le=: -.@{.@/:@,&boxopen NB. lexically less than or equal to

View file

@ -0,0 +1,6 @@
'ball' (eq , ne , gt , lt , ge , le) 'bell'
0 1 0 1 0 1
'ball' (eq , ne , gt , lt , ge , le) 'ball'
1 0 0 0 1 1
'YUP' (eq , ne , gt , lt , ge , le) 'YEP'
0 1 1 0 1 0

View file

@ -0,0 +1,38 @@
public class Compare
{
public static void main (String[] args)
{
compare("Hello", "Hello");
compare("5", "5.0");
compare("java", "Java");
compare("ĴÃVÁ", "ĴÃVÁ");
compare("ĴÃVÁ", "ĵãvá");
}
public static void compare (String A, String B)
{
if (A.equals(B))
System.out.printf("'%s' and '%s' are lexically equal.", A, B);
else
System.out.printf("'%s' and '%s' are not lexically equal.", A, B);
System.out.println();
if (A.equalsIgnoreCase(B))
System.out.printf("'%s' and '%s' are case-insensitive lexically equal.", A, B);
else
System.out.printf("'%s' and '%s' are not case-insensitive lexically equal.", A, B);
System.out.println();
if (A.compareTo(B) < 0)
System.out.printf("'%s' is lexically before '%s'.\n", A, B);
else if (A.compareTo(B) > 0)
System.out.printf("'%s' is lexically after '%s'.\n", A, B);
if (A.compareTo(B) >= 0)
System.out.printf("'%s' is not lexically before '%s'.\n", A, B);
if (A.compareTo(B) <= 0)
System.out.printf("'%s' is not lexically after '%s'.\n", A, B);
System.out.printf("The lexical relationship is: %d\n", A.compareTo(B));
System.out.printf("The case-insensitive lexical relationship is: %d\n\n", A.compareToIgnoreCase(B));
}
}

View file

@ -0,0 +1,22 @@
/*
== equal value
=== equal value and equal type
!= not equal value
!== not equal value or not equal type
< lexically ordered before
> lexically ordered after
*/
console.log(
"abcd" == "abcd", // true
"abcd" === "abcd", // true
123 == "123", // true
123 === "123", // false
"ABCD" == "abcd", // false
"ABCD" != "abcd", // true
123 != "123", // false
123 !== "123", // true
"abcd" < "dcba", // true
"abcd" > "dcba", // false
"ABCD".toLowerCase() == "abcd".toLowerCase(), // true (case insensitive)
)

View file

@ -0,0 +1,11 @@
# Comparing two strings for exact equality:
"this" == "this" # true
"this" == "This" # false
# != is the inverse of ==
# Comparing two strings to see if one is lexically ordered before the other:
"alpha" < "beta" # true
"beta" < "alpha" # false
# > is the inverse of <

View file

@ -0,0 +1 @@
("AtoZ" | ascii_upcase) == ("atoz" | ascii_upcase) # true

View file

@ -0,0 +1,17 @@
function compare(a, b)
println("\n$a is of type $(typeof(a)) and $b is of type $(typeof(b))")
if a < b println("$a is strictly less than $b") end
if a <= b println("$a is less than or equal to $b") end
if a > b println("$a is strictly greater than $b") end
if a >= b println("$a is greater than or equal to $b") end
if a == b println("$a is equal to $b") end
if a != b println("$a is not equal to $b") end
if a === b println("$a has object identity with $b") end
if a !== b println("$a has negated object identity with $b") end
end
compare("YUP", "YUP")
compare('a', 'z')
compare("24", "123")
compare(24, 123)
compare(5.0, 5)

View file

@ -0,0 +1,16 @@
// version 1.0.6
fun main(args: Array<String>) {
val k1 = "kotlin"
val k2 = "Kotlin"
println("Case sensitive comparisons:\n")
println("kotlin and Kotlin are equal = ${k1 == k2}")
println("kotlin and Kotlin are not equal = ${k1 != k2}")
println("kotlin comes before Kotlin = ${k1 < k2}")
println("kotlin comes after Kotlin = ${k1 > k2}")
println("\nCase insensitive comparisons:\n")
println("kotlin and Kotlin are equal = ${k1 == k2.toLowerCase()}")
println("kotlin and Kotlin are not equal = ${k1 != k2.toLowerCase()}")
println("kotlin comes before Kotlin = ${k1 < k2.toLowerCase()}")
println("kotlin comes after Kotlin = ${k1 > k2.toLowerCase()}")
}

View file

@ -0,0 +1,35 @@
// Comparing two strings for exact equality
"'this' == 'this': " + ('this' == 'this') // true
"'this' == 'This': " + ('this' == 'This') // true, as it's case insensitive
// Comparing two strings for inequality (i.e., the inverse of exact equality)
"'this' != 'this': " + ('this' != 'this')// false
"'this' != 'that': " + ('this' != 'that') // true
// Comparing two strings to see if one is lexically ordered before than the other
"'alpha' < 'beta': " + ('alpha' < 'beta') // true
"'beta' < 'alpha': " + ('beta' < 'alpha') // false
// Comparing two strings to see if one is lexically ordered after than the other
"'alpha' > 'beta': " + ('alpha' > 'beta') // false
"'beta' > 'alpha': " + ('beta' > 'alpha') // true
// How to achieve both case sensitive comparisons and case insensitive comparisons within the language
"case sensitive - 'this'->equals('This',-case=true): " + ('this'->equals('This',-case=true)) // false
"case insensitive - 'this'->equals('This',-case=true): " + ('this'->equals('This')) // true
// How the language handles comparison of numeric strings if these are not treated lexically
"'01234' == '01234': "+ ('01234' == '01234') // true
"'01234' == '0123': " + ('01234' == '0123') // false
"'01234' > '0123': " + ('01234' > '0123') // true
"'01234' < '0123': " + ('01234' < '0123') //false
// Additional string comparisons
"'The quick brown fox jumps over the rhino' >> 'fox' (contains): " +
('The quick brown fox jumps over the rhino' >> 'fox') // true
"'The quick brown fox jumps over the rhino' >> 'cat' (contains): " +
('The quick brown fox jumps over the rhino' >> 'cat') // false
"'The quick brown fox jumps over the rhino'->beginswith('rhino'): " +
('The quick brown fox jumps over the rhino'->beginswith('rhino')) // false
"'The quick brown fox jumps over the rhino'->endswith('rhino'): " +
('The quick brown fox jumps over the rhino'->endswith('rhino')) // true

View file

@ -0,0 +1,11 @@
put "abc"="ABC"
-- 1
put "abc"<>"def"
-- 1
put "abc"<"def"
-- 1
put "abc">"def"
-- 0

View file

@ -0,0 +1,10 @@
-- Returns -1 if str1 is less than str2
-- Returns 1 if str1 is greater than str2
-- Returns 0 if str1 and str2 are equal
on strcmp (str1, str2)
h1 = bytearray(str1).toHexString(1, str1.length)
h2 = bytearray(str2).toHexString(1, str2.length)
if h1<h2 then return -1
else if h1>h2 then return 1
return 0
end

View file

@ -0,0 +1,19 @@
function compare(a, b)
print(("%s is of type %s and %s is of type %s"):format(
a, type(a),
b, type(b)
))
if a < b then print(('%s is strictly less than %s'):format(a, b)) end
if a <= b then print(('%s is less than or equal to %s'):format(a, b)) end
if a > b then print(('%s is strictly greater than %s'):format(a, b)) end
if a >= b then print(('%s is greater than or equal to %s'):format(a, b)) end
if a == b then print(('%s is equal to %s'):format(a, b)) end
if a ~= b then print(('%s is not equal to %s'):format(a, b)) end
print ""
end
compare('YUP', 'YUP')
compare('BALL', 'BELL')
compare('24', '123')
compare(24, 123)
compare(5.0, 5)

View file

@ -0,0 +1,17 @@
a="BALL";
b="BELL";
if a==b, disp('The strings are equal'); end;
if strcmp(a,b), disp('The strings are equal'); end;
if a~=b, disp('The strings are not equal'); end;
if ~strcmp(a,b), disp('The strings are not equal'); end;
if a > b, disp('The first string is lexically after than the second'); end;
if a < b, disp('The first string is lexically before than the second'); end;
if a >= b, disp('The first string is not lexically before than the second'); end;
if a <= b, disp('The first string is not lexically after than the second'); end;
% to make a case insensitive comparison convert both strings to the same lettercase:
a="BALL";
b="ball";
if strcmpi(a,b), disp('The first and second string are the same disregarding letter case'); end;
if lower(a)==lower(b), disp('The first and second string are the same disregarding letter case'); end;

View file

@ -0,0 +1,19 @@
compare[x_, y_] := Module[{},
If[x == y,
Print["Comparing for equality (case sensitive): " <> x <> " and " <> y <> " ARE equal"],
Print["Comparing for equality (case sensitive): " <> x <> " and " <> y <> " are NOT equal" ]] ;
If[x != y,
Print["Comparing for inequality (case sensitive): " <> x <> " and " <> y <> " are NOT equal"],
Print["Comparing for inequality (case sensitive): " <> x <> " and " <> y <> " ARE equal" ]] ;
Switch[Order[x, y],
1, Print["Comparing for order (case sensitive): " <> x <> " comes before " <> y],
-1, Print["Comparing for order (case sensitive): " <> x <> " comes after " <> y],
0, Print["Comparing for order (case sensitive): " <> x <> " comes in the same spot as " <> y]];
If[ToLowerCase[x] == ToLowerCase[y],
Print["Comparing for equality (case insensitive): " <> x <> " and " <> y <> " ARE equal"],
Print["Comparing for equality (case insensitive): " <> x <> " and " <> y <> " are NOT equal" ]] ;
Print[];
]
compare["Hello", "Hello"]
compare["3.1", "3.14159"]
compare["mathematica", "Mathematica"]

View file

@ -0,0 +1,49 @@
string1 = input("Please enter a string.")
string2 = input("Please enter a second string.")
//Comparing two strings for exact equality
if string1 == string2 then
print "Strings are equal."
end if
//Comparing two strings for inequality
if string1 != string2 then
print "Strings are NOT equal."
end if
//Comparing two strings to see if one is lexically ordered before than the other
if string1 > string2 then
print string1 + " is lexically ordered AFTER " + string2
//Comparing two strings to see if one is lexically ordered after than the other
else if string1 < string2 then
print string1 + " is lexically ordered BEFORE " + string2
end if
//How to achieve case sensitive comparisons
//Comparing two strings for exact equality (case sensitive)
if string1 == string2 then
print "Strings are equal. (case sensitive)"
end if
//Comparing two strings for inequality (case sensitive)
if string1 != string2 then
print "Strings are NOT equal. (case sensitive)"
end if
//How to achieve case insensitive comparisons within the language
//Comparing two strings for exact equality (case insensitive)
if string1.lower == string2.lower then
print "Strings are equal. (case insensitive)"
end if
//Comparing two strings for inequality (case insensitive)
if string1.lower != string2.lower then
print "Strings are NOT equal. (case insensitive)"
end if

View file

@ -0,0 +1,27 @@
def compare(a, b)
println format("\n%s is of type %s and %s is of type %s", a, type(a), b, type(b))
if a < b
println format("%s is strictly less than %s", a, b)
end
if a <= b
println format("%s is less than or equal to %s", a, b)
end
if a > b
println format("%s is strictly greater than %s", a, b)
end
if a >= b
println format("%s is greater than or equal to %s", a, b)
end
if a = b
println format("%s is equal to %s", a, b)
end
if a != b
println format("%s is not equal to %s", a, b)
end
end
compare("YUP", "YUP")
compare("BALL", "BELL")
compare("24", "123")
compare(24, 123)
compare(5.0, 5)

View file

@ -0,0 +1,23 @@
animal = 'dog'
if animal = 'cat' then
say animal "is lexically equal to cat"
if animal \= 'cat' then
say animal "is not lexically equal cat"
if animal > 'cat' then
say animal "is lexically higher than cat"
if animal < 'cat' then
say animal "is lexically lower than cat"
if animal >= 'cat' then
say animal "is not lexically lower than cat"
if animal <= 'cat' then
say animal "is not lexically higher than cat"
/* The above comparative operators do not consider
leading and trailing whitespace when making comparisons. */
if ' cat ' = 'cat' then
say "this will print because whitespace is stripped"
/* To consider all whitespace in a comparison
we need to use strict comparative operators */
if ' cat ' == 'cat' then
say "this will not print because comparison is strict"

View file

@ -0,0 +1,12 @@
import strutils
var s1: string = "The quick brown"
var s2: string = "The Quick Brown"
echo("== : ", s1 == s2)
echo("!= : ", s1 != s2)
echo("< : ", s1 < s2)
echo("<= : ", s1 <= s2)
echo("> : ", s1 > s2)
echo(">= : ", s1 >= s2)
# cmpIgnoreCase(a, b) => 0 if a == b; < 0 if a < b; > 0 if a > b
echo("cmpIgnoreCase :", s1.cmpIgnoreCase s2)

View file

@ -0,0 +1,5 @@
"abcd" "abcd" ==
"abcd" "abce" <>
"abcd" "abceed" <=
"abce" "abcd" >
"abcEEE" toUpper "ABCeee" toUpper ==

View file

@ -0,0 +1,5 @@
a=.array~of('A 1','B 2','a 3','b 3','A 5')
a~sortwith(.caselesscomparator~new)
Do i=1 To 5
Say a[i]
End

View file

@ -0,0 +1,25 @@
use v5.16; # ...for fc(), which does proper Unicode casefolding.
# With older Perl versions you can use lc() as a poor-man's substitute.
sub compare {
my ($a, $b) = @_;
my $A = "'$a'";
my $B = "'$b'";
print "$A and $B are lexically equal.\n" if $a eq $b;
print "$A and $B are not lexically equal.\n" if $a ne $b;
print "$A is lexically before $B.\n" if $a lt $b;
print "$A is lexically after $B.\n" if $a gt $b;
print "$A is not lexically before $B.\n" if $a ge $b;
print "$A is not lexically after $B.\n" if $a le $b;
print "The lexical relationship is: ", $a cmp $b, "\n";
print "The case-insensitive lexical relationship is: ", fc($a) cmp fc($b), "\n";
print "\n";
}
compare('Hello', 'Hello');
compare('5', '5.0');
compare('perl', 'Perl');

View file

@ -0,0 +1,10 @@
(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"Pete"</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">==</span><span style="color: #008000;">"pete"</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #008000;">"The strings are equal"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">"pete"</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #008000;">"The strings are not equal"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">name</span><span style="color: #0000FF;"><</span><span style="color: #008000;">"pete"</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #008000;">"name is lexically first"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">></span><span style="color: #008000;">"pete"</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #008000;">"name is lexically last"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">upper</span><span style="color: #0000FF;">(</span><span style="color: #000000;">name</span><span style="color: #0000FF;">)=</span><span style="color: #7060A8;">upper</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"pete"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #008000;">"case insensitive match"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"pete"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">name</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #008000;">"petes in there somewhere"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--

View file

@ -0,0 +1,13 @@
/# Rosetta Code problem: https://rosettacode.org/wiki/String_comparison
by Galileo, 11/2022 #/
include ..\Utilitys.pmt
"Pete" >ps
"pete" tps == if "The strings are equal" ? endif
"pete" tps != if "The strings are not equal" ? endif
tps "pete" < if ( tps " is lexically first" ) lprint nl endif
tps "pete" > if ( tps " is lexically last" ) lprint nl endif
tps upper "pete" upper == if "case insensitive match" ? endif
ps> lower "pete" find if "petes in there somewhere" ? endif

View file

@ -0,0 +1,13 @@
main =>
S1 = "abc",
S2 = "def",
S1 == S2, % -> false.
S1 != S2, % -> true.
S1 @< S2, % -> true. Is S1 lexicographically less than S1?
S1 @> S2, % -> false.
to_lowercase(S1) == to_lowercase(S2), % -> false.
"1234" @> "123", % -> true. lexical comparison
"1234" @< 12342222, % -> false. No coersion is done. Numbers are always ordered before strings
123 < 1234. % -> true '<' is used only for numbers

View file

@ -0,0 +1,18 @@
(setq
str= =
str< <
str> > )
(println
(str= (lowc "Foo") (lowc "foo") (lowc "fOO"))
(str= "f" "foo")
(str= "foo" "foo" "foo")
(str= "" "") )
(println
(str< "abc" "def")
(str> "abc" "def")
(str< "" "")
(str< "12" "45") )
(bye)

View file

@ -0,0 +1,6 @@
"a" -lt "b" # lower than
"a" -eq "b" # equal
"a" -gt "b" # greater than
"a" -le "b" # lower than or equal
"a" -ne "b" # not equal
"a" -ge "b" # greater than or equal

View file

@ -0,0 +1,2 @@
"a" -eq "A"
"a" -ceq "A"

View file

@ -0,0 +1,37 @@
Macro StrTest(Check,tof)
Print("Test "+Check+#TAB$)
If tof=1 : PrintN("true") : Else : PrintN("false") : EndIf
EndMacro
Procedure.b StrBool_eq(a$,b$) : ProcedureReturn Bool(a$=b$) : EndProcedure
Procedure.b StrBool_n_eq(a$,b$) : ProcedureReturn Bool(a$<>b$) : EndProcedure
Procedure.b StrBool_a(a$,b$) : ProcedureReturn Bool(a$>b$) : EndProcedure
Procedure.b StrBool_b(a$,b$) : ProcedureReturn Bool(a$<b$) : EndProcedure
Procedure.b NumBool_eq(a$,b$) : ProcedureReturn Bool(Val(a$)=Val(b$)) : EndProcedure
Procedure.b NumBool_n_eq(a$,b$) : ProcedureReturn Bool(Val(a$)<>Val(b$)) : EndProcedure
Procedure.b NumBool_a(a$,b$) : ProcedureReturn Bool(Val(a$)>Val(b$)) : EndProcedure
Procedure.b NumBool_b(a$,b$) : ProcedureReturn Bool(Val(a$)<Val(b$)) : EndProcedure
Procedure Compare(a$,b$,cs.b=1,num.b=0)
If Not cs : a$=UCase(a$) : b$=UCase(b$) : EndIf
PrintN("a = "+a$) : PrintN("b = "+b$)
If Not num : StrTest(" a=b ",StrBool_eq(a$,b$)) : Else : StrTest(" a=b ",NumBool_eq(a$,b$)) : EndIf
If Not num : StrTest(" a<>b ",StrBool_n_eq(a$,b$)) : Else : StrTest(" a<>b ",NumBool_n_eq(a$,b$)) : EndIf
If Not num : StrTest(" a>b ",StrBool_a(a$,b$)) : Else : StrTest(" a>b ",NumBool_a(a$,b$)) : EndIf
If Not num : StrTest(" a<b ",StrBool_b(a$,b$)) : Else : StrTest(" a<b ",NumBool_b(a$,b$)) : EndIf
EndProcedure
If OpenConsole()
PrintN("String comparison - ")
a$="Abcd" : b$="abcd"
PrintN(#CRLF$+"- case sensitive:")
Compare(a$,b$)
PrintN(#CRLF$+"- case insensitive:")
Compare(a$,b$,0)
a$="1241" : b$="222"
PrintN(#CRLF$+"- num-string; lexically compared:")
Compare(a$,b$)
PrintN(#CRLF$+"- num-string; numerically compared:")
Compare(a$,b$,1,1)
Input()
EndIf

View file

@ -0,0 +1,17 @@
def compare(a, b):
print("\n%r is of type %r and %r is of type %r"
% (a, type(a), b, type(b)))
if a < b: print('%r is strictly less than %r' % (a, b))
if a <= b: print('%r is less than or equal to %r' % (a, b))
if a > b: print('%r is strictly greater than %r' % (a, b))
if a >= b: print('%r is greater than or equal to %r' % (a, b))
if a == b: print('%r is equal to %r' % (a, b))
if a != b: print('%r is not equal to %r' % (a, b))
if a is b: print('%r has object identity with %r' % (a, b))
if a is not b: print('%r has negated object identity with %r' % (a, b))
compare('YUP', 'YUP')
compare('BALL', 'BELL')
compare('24', '123')
compare(24, 123)
compare(5.0, 5)

View file

@ -0,0 +1,33 @@
Dim As String String1, String2
' direct string comparison using case sensitive
String1 = "GWbasic"
String2 = "QuickBasic"
If String1 = String2 Then Print String1; " is equal to "; String2 Else Print String1; " is NOT egual to "; String2
String1 = "gWbasic"
String2 = "GWBasic"
If String1 = String2 Then Print String1; " is equal to "; String2 Else Print String1; " is NOT egual to "; String2
' direct string comparison using case insensitive
If UCase$(String1) = UCase$(String2) Then Print String1; " is equal to "; String2; Else Print String1; " is NOT egual to "; String2;
Print " case insensitive"
String1 = "GwBasiC"
String2 = "GWBasic"
If LCase$(String1) = LCase$(String2) Then Print String1; " is equal to "; String2; Else Print String1; " is NOT egual to "; String2;
Print " case insensitive"
' lexical order
String1 = "AAAbbb"
String2 = "AaAbbb"
If String1 > String2 Then Print String1; " is after "; String2 Else Print String1; " is before "; String2
' number in string format comparison
String1 = "0123"
String2 = "5"
' lexical order
If String1 > String2 Then Print String1; " is after "; String2 Else Print String1; " is before "; String2
' value order
If Val(String1) > Val(String2) Then Print String1; " is bigger than "; String2 Else Print String1; " is lower "; String2
Print "QB64, like QBasic, has native coercive/allomorphic operators for string type variable"
End

View file

@ -0,0 +1,29 @@
FUNCTION StringCompare$ (s1 AS STRING, s2 AS STRING, ignoreCase)
DIM s AS STRING, t AS STRING
IF ignoreCase THEN
s = LCASE$(s1)
t = LCASE$(s2)
ELSE
s = s1
t = s2
END IF
IF s < t THEN StringCompare$ = " comes before ": EXIT FUNCTION
IF s = t THEN StringCompare$ = " is equal to ": EXIT FUNCTION
StringCompare$ = " comes after "
END FUNCTION
DIM s1 AS STRING, s2 AS STRING, s3 AS STRING
s1 = "Dog": s2 = "Dog"
PRINT s1; StringCompare$(s1, s2, 0); s2
s2 = "Cat"
PRINT s1; StringCompare$(s1, s2, 0); s2
s2 = "Rat"
PRINT s1; StringCompare$(s1, s2, 0); s2
s2 = "dog"
PRINT s1; StringCompare$(s1, s2, 0); s2
PRINT s1; StringCompare$(s1, s2, 1); s2; " if case is ignored"
s1 = "Dog": s2 = "Pig"
s3 = StringCompare$(s1, s2, 0)
IF s3 <> " is equal to " THEN PRINT s1; " is not equal to "; s2
END

View file

@ -0,0 +1,19 @@
compare <- function(a, b)
{
cat(paste(a, "is of type", class(a), "and", b, "is of type", class(b), "\n"))
if (a < b) cat(paste(a, "is strictly less than", b, "\n"))
if (a <= b) cat(paste(a, "is less than or equal to", b, "\n"))
if (a > b) cat(paste(a, "is strictly greater than", b, "\n"))
if (a >= b) cat(paste(a, "is greater than or equal to", b, "\n"))
if (a == b) cat(paste(a, "is equal to", b, "\n"))
if (a != b) cat(paste(a, "is not equal to", b, "\n"))
invisible()
}
compare('YUP', 'YUP')
compare('BALL', 'BELL')
compare('24', '123')
compare(24, 123)
compare(5.0, 5)

View file

@ -0,0 +1,20 @@
compare <- function(a, b)
{
cat(paste(a, "is of type", class(a), "and", b, "is of type", class(b), "\n"))
printer <- function(a, b, msg) cat(paste(a, msg, b, "\n"))
op <- c(`<`, `<=`, `>`, `>=`, `==`, `!=`)
msgs <- c(
"is strictly less than",
"is less than or equal to",
"is strictly greater than",
"is greater than or equal to",
"is equal to",
"is not equal to"
)
sapply(1:length(msgs), function(i) if(op[[i]](a, b)) printer(a, b, msgs[i]))
invisible()
}

View file

@ -0,0 +1,37 @@
/*REXX program shows different ways to compare two character strings.*/
say 'This is an ' word('ASCII EBCDIC', 1+(1=='f1')) ' system.'
say
cat = 'cat'
animal = 'dog'
if animal = cat then say $(animal) "is lexically equal to" $(cat)
if animal \= cat then say $(animal) "is not lexically equal to" $(cat)
if animal > cat then say $(animal) "is lexically higher than" $(cat)
if animal < cat then say $(animal) "is lexically lower than" $(cat)
if animal > cat then say $(animal) "is not lexically lower than" $(cat)
if animal < cat then say $(animal) "is not lexically higher than" $(cat)
/*──── [↑] The above comparative operators don't */
/*────consider any leading and/or trailing white- */
/*────space when making comparisons, but the case */
/*────is honored (uppercase, lowercase). */
fatcat=' cat ' /*pad the cat with leading and trailing blanks. */
if fatcat = cat then say $(fatcat) " is equal to" $(cat)
/*────To consider any whitespace in a comparison, */
/*────we need to use strict comparative operators.*/
if fatcat == cat then say $(fatcat) "is strictly equal to" $(cat)
/*────To perform caseless comparisons, the easiest*/
/*────method would be to uppercase a copy of both */
/*────operands. Uppercasing is only done for the */
/*────Latin (or Roman) alphabet in REXX. [↓] */
kat='cAt'
if caselessComp(cat,kat) then say $(cat) 'and' $(kat) "are equal caseless"
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────$ subroutine────────────────────────*/
$: return ''arg(1)'' /*bracket the string with ──►α◄──*/
/*──────────────────────────────────CASELESSCOMP subroutine─────────────*/
caselessComp: procedure; arg a,b /*ARG uppercases the A & B args.*/
return a==b /*if exactly equal, return 1. */

View file

@ -0,0 +1,23 @@
/* REXX ***************************************************************
* 16.05.2013 Walter Pachl
**********************************************************************/
Call test 'A','<','a'
Call test 'A','=',' a'
Call test 'A','==',' a'
Call test 'Walter','<',' Wolter'
Exit
test: Procedure
Parse Arg o1,op,o2
Say q(o1) op q(o2) '->' clcompare(o1,op,o2)
Return
clcompare: Procedure
/* caseless comparison of the operands */
Parse Arg opd1,op,opd2
opd1u=translate(opd1)
opd2u=translate(opd2)
Interpret 'res=opd1u' op 'opd2u'
Return res
q: Return '"'arg(1)'"'

Some files were not shown because too many files have changed in this diff Show more