Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.StringComparer F# snippets #7876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
open System

// <Snippet1>
let compareCurrentCultureStringComparer () =
let stringComparer1 = StringComparer.CurrentCulture
let stringComparer2 = StringComparer.CurrentCulture
// Displays false
printfn $"{StringComparer.ReferenceEquals(stringComparer1, stringComparer2)}"
// </Snippet1>

// <Snippet2>
let compareCurrentCultureInsensitiveStringComparer () =
let stringComparer1 = StringComparer.CurrentCultureIgnoreCase
let stringComparer2 = StringComparer.CurrentCultureIgnoreCase
// Displays false
printfn $"{StringComparer.ReferenceEquals(stringComparer1, stringComparer2)}"
// </Snippet2>
compareCurrentCultureStringComparer()
compareCurrentCultureInsensitiveStringComparer()
10 changes: 10 additions & 0 deletions snippets/fsharp/System/StringComparer/CurrentCulture/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="CompareObjects.fs" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/StringComparer/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="omni.fs" />
</ItemGroup>
</Project>
125 changes: 125 additions & 0 deletions snippets/fsharp/System/StringComparer/Overview/omni.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//<snippet1>
// This example demonstrates members of the
// System.StringComparer class.

open System
open System.Globalization
open System.Threading

let display (lst: ResizeArray<string>) 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

*)
//</snippet1>
8 changes: 8 additions & 0 deletions xml/System/StringComparer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ The following example demonstrates the properties and the <xref:System.StringCom

:::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":::

]]></format>
Expand Down Expand Up @@ -357,6 +358,7 @@ The following example demonstrates the properties and the <xref:System.StringCom

:::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":::

]]></format>
Expand Down Expand Up @@ -464,6 +466,7 @@ The following example demonstrates the properties and the <xref:System.StringCom
Each call to the <xref:System.StringComparer.CurrentCulture%2A> property `get` accessor returns a new <xref:System.StringComparer> 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 <xref:System.StringComparer> object in a local variable rather than retrieve the value of the <xref:System.StringComparer.CurrentCulture%2A> property multiple times.
Expand All @@ -475,6 +478,7 @@ The following example demonstrates the properties and the <xref:System.StringCom

:::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":::

]]></format>
Expand Down Expand Up @@ -537,6 +541,7 @@ The following example demonstrates the properties and the <xref:System.StringCom
Each call to the <xref:System.StringComparer.CurrentCultureIgnoreCase%2A> property `get` accessor returns a new <xref:System.StringComparer> 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 <xref:System.StringComparer> object in a local variable rather than retrieve the value of the <xref:System.StringComparer.CurrentCultureIgnoreCase%2A> property multiple times.
Expand Down Expand Up @@ -905,6 +910,7 @@ The following example demonstrates the properties and the <xref:System.StringCom

:::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":::

]]></format>
Expand Down Expand Up @@ -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":::

]]></format>
Expand Down Expand Up @@ -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":::

]]></format>
Expand Down