Skip to content

Commit 8ffd9cc

Browse files
authored
StringComparer F# snippets (dotnet#7876)
1 parent 26ce7bb commit 8ffd9cc

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
open System
2+
3+
// <Snippet1>
4+
let compareCurrentCultureStringComparer () =
5+
let stringComparer1 = StringComparer.CurrentCulture
6+
let stringComparer2 = StringComparer.CurrentCulture
7+
// Displays false
8+
printfn $"{StringComparer.ReferenceEquals(stringComparer1, stringComparer2)}"
9+
// </Snippet1>
10+
11+
// <Snippet2>
12+
let compareCurrentCultureInsensitiveStringComparer () =
13+
let stringComparer1 = StringComparer.CurrentCultureIgnoreCase
14+
let stringComparer2 = StringComparer.CurrentCultureIgnoreCase
15+
// Displays false
16+
printfn $"{StringComparer.ReferenceEquals(stringComparer1, stringComparer2)}"
17+
// </Snippet2>
18+
compareCurrentCultureStringComparer()
19+
compareCurrentCultureInsensitiveStringComparer()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="CompareObjects.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="omni.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//<snippet1>
2+
// This example demonstrates members of the
3+
// System.StringComparer class.
4+
5+
open System
6+
open System.Globalization
7+
open System.Threading
8+
9+
let display (lst: ResizeArray<string>) title =
10+
printfn $"%s{title}"
11+
for s in lst do
12+
let c = s[0]
13+
let codePoint = Convert.ToInt32 c
14+
printfn $"0x{codePoint:x}"
15+
printfn ""
16+
17+
// Create a list of string.
18+
let list = ResizeArray()
19+
20+
// Get the tr-TR (Turkish-Turkey) culture.
21+
let turkish = CultureInfo "tr-TR"
22+
23+
// Get the culture that is associated with the current thread.
24+
let thisCulture = Thread.CurrentThread.CurrentCulture
25+
26+
// Get the standard StringComparers.
27+
let invCmp = StringComparer.InvariantCulture
28+
let invICCmp = StringComparer.InvariantCultureIgnoreCase
29+
let currCmp = StringComparer.CurrentCulture
30+
let currICCmp = StringComparer.CurrentCultureIgnoreCase
31+
let ordCmp = StringComparer.Ordinal
32+
let ordICCmp = StringComparer.OrdinalIgnoreCase
33+
34+
// Create a StringComparer that uses the Turkish culture and ignores case.
35+
let turkICComp = StringComparer.Create(turkish, true)
36+
37+
// Define three strings consisting of different versions of the letter I.
38+
// LATIN CAPITAL LETTER I (U+0049)
39+
let capitalLetterI = "I"
40+
41+
// LATIN SMALL LETTER I (U+0069)
42+
let smallLetterI = "i"
43+
44+
// LATIN SMALL LETTER DOTLESS I (U+0131)
45+
let smallLetterDotlessI = "\u0131"
46+
47+
// Add the three strings to the list.
48+
list.Add capitalLetterI
49+
list.Add smallLetterI
50+
list.Add smallLetterDotlessI
51+
52+
// Display the original list order.
53+
display list "The original order of the list entries..."
54+
55+
// Sort the list using the invariant culture.
56+
list.Sort invCmp
57+
display list "Invariant culture..."
58+
list.Sort invICCmp
59+
display list "Invariant culture, ignore case..."
60+
61+
// Sort the list using the current culture.
62+
printfn $"The current culture is \"{thisCulture.Name}\"."
63+
list.Sort currCmp
64+
display list "Current culture..."
65+
list.Sort currICCmp
66+
display list "Current culture, ignore case..."
67+
68+
// Sort the list using the ordinal value of the character code points.
69+
list.Sort ordCmp
70+
display list "Ordinal..."
71+
list.Sort ordICCmp
72+
display list "Ordinal, ignore case..."
73+
74+
// Sort the list using the Turkish culture, which treats LATIN SMALL LETTER
75+
// DOTLESS I differently than LATIN SMALL LETTER I.
76+
list.Sort turkICComp
77+
display list "Turkish culture, ignore case..."
78+
79+
80+
(*
81+
This code example produces the following results:
82+
83+
The original order of the list entries...
84+
0x49
85+
0x69
86+
0x131
87+
88+
Invariant culture...
89+
0x69
90+
0x49
91+
0x131
92+
93+
Invariant culture, ignore case...
94+
0x49
95+
0x69
96+
0x131
97+
98+
The current culture is "en-US".
99+
Current culture...
100+
0x69
101+
0x49
102+
0x131
103+
104+
Current culture, ignore case...
105+
0x49
106+
0x69
107+
0x131
108+
109+
Ordinal...
110+
0x49
111+
0x69
112+
0x131
113+
114+
Ordinal, ignore case...
115+
0x69
116+
0x49
117+
0x131
118+
119+
Turkish culture, ignore case...
120+
0x131
121+
0x49
122+
0x69
123+
124+
*)
125+
//</snippet1>

xml/System/StringComparer.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ The following example demonstrates the properties and the <xref:System.StringCom
9494
9595
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.StringComparer/cpp/omni.cpp" id="Snippet1":::
9696
:::code language="csharp" source="~/snippets/csharp/System/StringComparer/Overview/omni.cs" id="Snippet1":::
97+
:::code language="fsharp" source="~/snippets/fsharp/System/StringComparer/Overview/omni.fs" id="Snippet1":::
9798
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.StringComparer/vb/omni.vb" id="Snippet1":::
9899
99100
]]></format>
@@ -357,6 +358,7 @@ The following example demonstrates the properties and the <xref:System.StringCom
357358
358359
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.StringComparer/cpp/omni.cpp" id="Snippet1":::
359360
:::code language="csharp" source="~/snippets/csharp/System/StringComparer/Overview/omni.cs" id="Snippet1":::
361+
:::code language="fsharp" source="~/snippets/fsharp/System/StringComparer/Overview/omni.fs" id="Snippet1":::
360362
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.StringComparer/vb/omni.vb" id="Snippet1":::
361363
362364
]]></format>
@@ -464,6 +466,7 @@ The following example demonstrates the properties and the <xref:System.StringCom
464466
Each call to the <xref:System.StringComparer.CurrentCulture%2A> property `get` accessor returns a new <xref:System.StringComparer> object, as the following code shows.
465467
466468
:::code language="csharp" source="~/snippets/csharp/System/StringComparer/CurrentCulture/CompareObjects.cs" id="Snippet1":::
469+
:::code language="fsharp" source="~/snippets/fsharp/System/StringComparer/CurrentCulture/CompareObjects.fs" id="Snippet1":::
467470
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.StringComparer.CurrentCulture/vb/CompareObjects.vb" id="Snippet1":::
468471
469472
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.
@@ -475,6 +478,7 @@ The following example demonstrates the properties and the <xref:System.StringCom
475478
476479
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.StringComparer/cpp/omni.cpp" id="Snippet1":::
477480
:::code language="csharp" source="~/snippets/csharp/System/StringComparer/Overview/omni.cs" id="Snippet1":::
481+
:::code language="fsharp" source="~/snippets/fsharp/System/StringComparer/Overview/omni.fs" id="Snippet1":::
478482
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.StringComparer/vb/omni.vb" id="Snippet1":::
479483
480484
]]></format>
@@ -537,6 +541,7 @@ The following example demonstrates the properties and the <xref:System.StringCom
537541
Each call to the <xref:System.StringComparer.CurrentCultureIgnoreCase%2A> property `get` accessor returns a new <xref:System.StringComparer> object, as the following code shows.
538542
539543
:::code language="csharp" source="~/snippets/csharp/System/StringComparer/CurrentCulture/CompareObjects.cs" id="Snippet2":::
544+
:::code language="fsharp" source="~/snippets/fsharp/System/StringComparer/CurrentCulture/CompareObjects.fs" id="Snippet2":::
540545
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.StringComparer.CurrentCulture/vb/CompareObjects.vb" id="Snippet2":::
541546
542547
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.
@@ -905,6 +910,7 @@ The following example demonstrates the properties and the <xref:System.StringCom
905910
906911
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.StringComparer/cpp/omni.cpp" id="Snippet1":::
907912
:::code language="csharp" source="~/snippets/csharp/System/StringComparer/Overview/omni.cs" id="Snippet1":::
913+
:::code language="fsharp" source="~/snippets/fsharp/System/StringComparer/Overview/omni.fs" id="Snippet1":::
908914
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.StringComparer/vb/omni.vb" id="Snippet1":::
909915
910916
]]></format>
@@ -1130,6 +1136,7 @@ A "well-known ordinal comparer" describes a comparer that behaves identically to
11301136
11311137
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.StringComparer/cpp/omni.cpp" id="Snippet1":::
11321138
:::code language="csharp" source="~/snippets/csharp/System/StringComparer/Overview/omni.cs" id="Snippet1":::
1139+
:::code language="fsharp" source="~/snippets/fsharp/System/StringComparer/Overview/omni.fs" id="Snippet1":::
11331140
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.StringComparer/vb/omni.vb" id="Snippet1":::
11341141
11351142
]]></format>
@@ -1192,6 +1199,7 @@ A "well-known ordinal comparer" describes a comparer that behaves identically to
11921199
11931200
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.StringComparer/cpp/omni.cpp" id="Snippet1":::
11941201
:::code language="csharp" source="~/snippets/csharp/System/StringComparer/Overview/omni.cs" id="Snippet1":::
1202+
:::code language="fsharp" source="~/snippets/fsharp/System/StringComparer/Overview/omni.fs" id="Snippet1":::
11951203
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.StringComparer/vb/omni.vb" id="Snippet1":::
11961204
11971205
]]></format>

0 commit comments

Comments
 (0)