diff --git a/snippets/fsharp/System/StringComparer/CurrentCulture/CompareObjects.fs b/snippets/fsharp/System/StringComparer/CurrentCulture/CompareObjects.fs new file mode 100644 index 00000000000..7de63e35286 --- /dev/null +++ b/snippets/fsharp/System/StringComparer/CurrentCulture/CompareObjects.fs @@ -0,0 +1,19 @@ +open System + +// +let compareCurrentCultureStringComparer () = + let stringComparer1 = StringComparer.CurrentCulture + let stringComparer2 = StringComparer.CurrentCulture + // Displays false + printfn $"{StringComparer.ReferenceEquals(stringComparer1, stringComparer2)}" +// + +// +let compareCurrentCultureInsensitiveStringComparer () = + let stringComparer1 = StringComparer.CurrentCultureIgnoreCase + let stringComparer2 = StringComparer.CurrentCultureIgnoreCase + // Displays false + printfn $"{StringComparer.ReferenceEquals(stringComparer1, stringComparer2)}" +// +compareCurrentCultureStringComparer() +compareCurrentCultureInsensitiveStringComparer() \ No newline at end of file diff --git a/snippets/fsharp/System/StringComparer/CurrentCulture/fs.fsproj b/snippets/fsharp/System/StringComparer/CurrentCulture/fs.fsproj new file mode 100644 index 00000000000..084210fefba --- /dev/null +++ b/snippets/fsharp/System/StringComparer/CurrentCulture/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/StringComparer/Overview/fs.fsproj b/snippets/fsharp/System/StringComparer/Overview/fs.fsproj new file mode 100644 index 00000000000..2de995757fd --- /dev/null +++ b/snippets/fsharp/System/StringComparer/Overview/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/StringComparer/Overview/omni.fs b/snippets/fsharp/System/StringComparer/Overview/omni.fs new file mode 100644 index 00000000000..390a7c75179 --- /dev/null +++ b/snippets/fsharp/System/StringComparer/Overview/omni.fs @@ -0,0 +1,125 @@ +// +// This example demonstrates members of the +// System.StringComparer class. + +open System +open System.Globalization +open System.Threading + +let display (lst: ResizeArray) title = + printfn $"%s{title}" + for s in lst do + let c = s[0] + let codePoint = Convert.ToInt32 c + printfn $"0x{codePoint:x}" + printfn "" + +// Create a list of string. +let list = ResizeArray() + +// Get the tr-TR (Turkish-Turkey) culture. +let turkish = CultureInfo "tr-TR" + +// Get the culture that is associated with the current thread. +let thisCulture = Thread.CurrentThread.CurrentCulture + +// Get the standard StringComparers. +let invCmp = StringComparer.InvariantCulture +let invICCmp = StringComparer.InvariantCultureIgnoreCase +let currCmp = StringComparer.CurrentCulture +let currICCmp = StringComparer.CurrentCultureIgnoreCase +let ordCmp = StringComparer.Ordinal +let ordICCmp = StringComparer.OrdinalIgnoreCase + +// Create a StringComparer that uses the Turkish culture and ignores case. +let turkICComp = StringComparer.Create(turkish, true) + +// Define three strings consisting of different versions of the letter I. +// LATIN CAPITAL LETTER I (U+0049) +let capitalLetterI = "I" + +// LATIN SMALL LETTER I (U+0069) +let smallLetterI = "i" + +// LATIN SMALL LETTER DOTLESS I (U+0131) +let smallLetterDotlessI = "\u0131" + +// Add the three strings to the list. +list.Add capitalLetterI +list.Add smallLetterI +list.Add smallLetterDotlessI + +// Display the original list order. +display list "The original order of the list entries..." + +// Sort the list using the invariant culture. +list.Sort invCmp +display list "Invariant culture..." +list.Sort invICCmp +display list "Invariant culture, ignore case..." + +// Sort the list using the current culture. +printfn $"The current culture is \"{thisCulture.Name}\"." +list.Sort currCmp +display list "Current culture..." +list.Sort currICCmp +display list "Current culture, ignore case..." + +// Sort the list using the ordinal value of the character code points. +list.Sort ordCmp +display list "Ordinal..." +list.Sort ordICCmp +display list "Ordinal, ignore case..." + +// Sort the list using the Turkish culture, which treats LATIN SMALL LETTER +// DOTLESS I differently than LATIN SMALL LETTER I. +list.Sort turkICComp +display list "Turkish culture, ignore case..." + + +(* +This code example produces the following results: + +The original order of the list entries... +0x49 +0x69 +0x131 + +Invariant culture... +0x69 +0x49 +0x131 + +Invariant culture, ignore case... +0x49 +0x69 +0x131 + +The current culture is "en-US". +Current culture... +0x69 +0x49 +0x131 + +Current culture, ignore case... +0x49 +0x69 +0x131 + +Ordinal... +0x49 +0x69 +0x131 + +Ordinal, ignore case... +0x69 +0x49 +0x131 + +Turkish culture, ignore case... +0x131 +0x49 +0x69 + +*) +// \ No newline at end of file diff --git a/xml/System/StringComparer.xml b/xml/System/StringComparer.xml index 2bf1e93c486..8aedfd0403e 100644 --- a/xml/System/StringComparer.xml +++ b/xml/System/StringComparer.xml @@ -94,6 +94,7 @@ The following example demonstrates the properties and the @@ -357,6 +358,7 @@ The following example demonstrates the properties and the @@ -464,6 +466,7 @@ The following example demonstrates the properties and the property `get` accessor returns a new object, as the following code shows. :::code language="csharp" source="~/snippets/csharp/System/StringComparer/CurrentCulture/CompareObjects.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/StringComparer/CurrentCulture/CompareObjects.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.StringComparer.CurrentCulture/vb/CompareObjects.vb" id="Snippet1"::: To improve performance, you can store the object in a local variable rather than retrieve the value of the property multiple times. @@ -475,6 +478,7 @@ The following example demonstrates the properties and the @@ -537,6 +541,7 @@ The following example demonstrates the properties and the property `get` accessor returns a new object, as the following code shows. :::code language="csharp" source="~/snippets/csharp/System/StringComparer/CurrentCulture/CompareObjects.cs" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/StringComparer/CurrentCulture/CompareObjects.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.StringComparer.CurrentCulture/vb/CompareObjects.vb" id="Snippet2"::: To improve performance, you can store the object in a local variable rather than retrieve the value of the property multiple times. @@ -905,6 +910,7 @@ The following example demonstrates the properties and the @@ -1130,6 +1136,7 @@ A "well-known ordinal comparer" describes a comparer that behaves identically to :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.StringComparer/cpp/omni.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/StringComparer/Overview/omni.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/StringComparer/Overview/omni.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.StringComparer/vb/omni.vb" id="Snippet1"::: ]]> @@ -1192,6 +1199,7 @@ A "well-known ordinal comparer" describes a comparer that behaves identically to :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.StringComparer/cpp/omni.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/StringComparer/Overview/omni.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/StringComparer/Overview/omni.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.StringComparer/vb/omni.vb" id="Snippet1"::: ]]>