Jump to content

String comparison: Difference between revisions

Added F# version
(Added F# version)
Line 242:
}</lang>
 
 
=={{header|F_Sharp|F#}}==
As a .NET language F# can make use of the [http://msdn.microsoft.com/en-us/library/system.string System.String] class.
As strict strongly typed language F# never coerces any other type to string.
<tt>System.String</tt> implements <tt>CompareTo</tt> function variants which are told by a
[http://msdn.microsoft.com/en-us/library/system.stringcomparison StringComparison] enumeration value how to compare, which might be "culture sensitive" or use an "ordinal comparison".
Both of these might also be of the <tt>IgnoreCase</tt> variant.
<lang fsharp>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</lang>
Output
<pre style="font-size:smaller">YUP is less than or equal to YUP
YUP is greater than or equal to YUP
YUP is equal to YUP
YUP is less than or equal to YUP (case insensitive)
YUP is greater than or equal to YUP (case insensitive)
YUP is equal to YUP (case insensitive)
BALL is strictly less than BELL
BALL is less than or equal to BELL
BALL is not equal to BELL
BALL is strictly less than BELL (case insensitive)
BALL is less than or equal to BELL (case insensitive)
BALL is not equal to BELL (case insensitive)
24 is strictly greater than 123
24 is greater than or equal to 123
24 is not equal to 123
24 is strictly greater than 123 (case insensitive)
24 is greater than or equal to 123 (case insensitive)
24 is not equal to 123 (case insensitive)
BELL is strictly less than bELL
BELL is less than or equal to bELL
BELL is not equal to bELL
BELL is less than or equal to bELL (case insensitive)
BELL is greater than or equal to bELL (case insensitive)
BELL is equal to bELL (case insensitive)</pre>
 
=={{header|J}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.