Skip to content

Commit 1558662

Browse files
authored
System.Char F# snippets (#7535)
* System.Char F# snippets * fix build * Update utf.fs
1 parent 4ea5835 commit 1558662

File tree

61 files changed

+1161
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1161
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="tp.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
//<snippet1>
2+
// This example demonstrates overloads of the TryParse method for
3+
// several base types, and the TryParseExact method for DateTime.
4+
5+
// In most cases, this example uses the most complex overload that is, the overload
6+
// with the most parameters for a particular type. If a complex overload specifies
7+
// null (Nothing in Visual Basic) for the IFormatProvider parameter, formatting
8+
// information is obtained from the culture associated with the current thread.
9+
// If a complex overload specifies the style parameter, the parameter value is
10+
// the default value used by the equivalent simple overload.
11+
12+
open System
13+
open System.Globalization
14+
15+
let show parseSuccess typeName parseValue =
16+
if parseSuccess then
17+
printfn $"Parse for %s{typeName} = %s{parseValue}"
18+
else
19+
printfn $"** Parse for %s{typeName} failed. Invalid input."
20+
21+
[<EntryPoint>]
22+
let main _ =
23+
printfn "This example demonstrates overloads of the TryParse method for\nseveral base types, as well as the TryParseExact method for DateTime.\n"
24+
25+
// Non-numeric types:
26+
printfn "Non-numeric types:\n"
27+
// DateTime
28+
// TryParse:
29+
// Assume current culture is en-US, and dates of the form: MMDDYYYY.
30+
let success, datetimeVal = DateTime.TryParse "7/4/2004 12:34:56"
31+
show success "DateTime #1" (string datetimeVal)
32+
33+
// Use fr-FR culture, and dates of the form: DDMMYYYY.
34+
let ci = CultureInfo "fr-FR"
35+
let success, datetimeVal = DateTime.TryParse("4/7/2004 12:34:56", ci, DateTimeStyles.None)
36+
show success "DateTime #2" (string datetimeVal)
37+
38+
// TryParseExact:
39+
// Use fr-FR culture. The format, "G", is short date and long time.
40+
let success, datetimeVal = DateTime.TryParseExact("04/07/2004 12:34:56", "G", ci, DateTimeStyles.None)
41+
show success "DateTime #3" (string datetimeVal)
42+
43+
// Assume en-US culture.
44+
let dateFormats = [| "f"; "F"; "g"; "G" |]
45+
let success, datetimeVal = DateTime.TryParseExact("7/4/2004 12:34:56 PM", dateFormats, null, DateTimeStyles.None)
46+
show success "DateTime #4" (string datetimeVal)
47+
48+
printfn ""
49+
// Boolean
50+
let success, booleanVal = Boolean.TryParse "true"
51+
show success "Boolean" (string booleanVal)
52+
// Char
53+
let success, charVal = Char.TryParse "A"
54+
show success "Char" (string charVal)
55+
56+
// Numeric types:
57+
printfn "\nNumeric types:\n"
58+
// Byte
59+
let success, byteVal = Byte.TryParse("1", NumberStyles.Integer, null)
60+
show success "Byte" (string byteVal)
61+
// Int16
62+
let success, int16Val = Int16.TryParse("-2", NumberStyles.Integer, null)
63+
show success "Int16" (string int16Val)
64+
// Int32
65+
let success, int32Val = Int32.TryParse("3", NumberStyles.Integer, null)
66+
show success "Int32" (string int32Val)
67+
// Int64
68+
let success, int64Val = Int64.TryParse("4", NumberStyles.Integer, null)
69+
show success "Int64" (string int64Val)
70+
// Decimal
71+
let success, decimalVal = Decimal.TryParse("-5.5", NumberStyles.Number, null)
72+
show success "Decimal" (string decimalVal)
73+
// Single
74+
let success, singleVal = Single.TryParse("6.6", NumberStyles.Float ||| NumberStyles.AllowThousands, null)
75+
show success "Single" (string singleVal)
76+
// Double
77+
let success, doubleVal = Double.TryParse("-7", NumberStyles.Float ||| NumberStyles.AllowThousands, null)
78+
show success "Double" (string doubleVal)
79+
80+
// Use the simple Double.TryParse overload, but specify an invalid value.
81+
82+
let success, doubleVal = Double.TryParse "abc"
83+
show success "Double #2" (string doubleVal)
84+
//
85+
printfn "\nThe following types are not CLS-compliant:\n"
86+
// SByte
87+
let success, sbyteVal = SByte.TryParse("-8", NumberStyles.Integer, null)
88+
show success "SByte" (string sbyteVal)
89+
// UInt16
90+
let success, uint16Val = UInt16.TryParse("9", NumberStyles.Integer, null)
91+
show success "UInt16" (string uint16Val)
92+
// UInt32
93+
let success, uint32Val = UInt32.TryParse("10", NumberStyles.Integer, null)
94+
show success "UInt32" (string uint32Val)
95+
// UInt64
96+
let success, uint64Val = UInt64.TryParse("11", NumberStyles.Integer, null)
97+
show success "UInt64" (string uint64Val)
98+
99+
0
100+
101+
// This example produces the following results:
102+
//
103+
// This example demonstrates overloads of the TryParse method for
104+
// several base types, as well as the TryParseExact method for DateTime.
105+
//
106+
// Non-numeric types:
107+
//
108+
// Parse for DateTime #1 = 7/4/2004 12:34:56
109+
// Parse for DateTime #2 = 7/4/2004 12:34:56
110+
// Parse for DateTime #3 = 7/4/2004 12:34:56
111+
// Parse for DateTime #4 = 7/4/2004 12:34:56
112+
//
113+
// Parse for Boolean = True
114+
// Parse for Char = A
115+
//
116+
// Numeric types:
117+
//
118+
// Parse for Byte = 1
119+
// Parse for Int16 = -2
120+
// Parse for Int32 = 3
121+
// Parse for Int64 = 4
122+
// Parse for Decimal = -5.5
123+
// Parse for Single = 6.6
124+
// Parse for Double = -7
125+
// ** Parse for Double #2 failed. Invalid input.
126+
//
127+
// The following types are not CLS-compliant:
128+
//
129+
// Parse for SByte = -8
130+
// Parse for UInt16 = 9
131+
// Parse for UInt32 = 10
132+
// Parse for UInt64 = 11
133+
//</snippet1>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="utf.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//<snippet1>
2+
open System
3+
4+
let show (s: string) =
5+
for x = 0 to s.Length - 1 do
6+
printf $"""0x{int s[x]:X}{if x = s.Length - 1 then String.Empty else ", "}"""
7+
8+
[<EntryPoint>]
9+
let main _ =
10+
let letterA = 0x0041 //U+00041 = LATIN CAPITAL LETTER A
11+
let music = 0x1D161 //U+1D161 = MUSICAL SYMBOL SIXTEENTH NOTE
12+
let comment = "Create a UTF-16 encoded string from a code point."
13+
let comment1b = "Create a code point from a UTF-16 encoded string."
14+
let comment2b = "Create a code point from a surrogate pair at a certain position in a string."
15+
let comment2c = "Create a code point from a high surrogate and a low surrogate code point."
16+
17+
// Convert code point U+0041 to UTF-16. The UTF-16 equivalent of
18+
// U+0041 is a Char with hexadecimal value 0041.
19+
20+
printfn $"{comment}"
21+
let s1 = Char.ConvertFromUtf32 letterA
22+
printf $" 1a) 0x{letterA:X} => "
23+
show s1
24+
printfn ""
25+
26+
// Convert the lone UTF-16 character to a code point.
27+
28+
printfn $"{comment1b}"
29+
let letterA = Char.ConvertToUtf32(s1, 0)
30+
printf " 1b) "
31+
show s1
32+
printfn $" => 0x{letterA:X}"
33+
printfn ""
34+
35+
// -------------------------------------------------------------------
36+
37+
// Convert the code point U+1D161 to UTF-16. The UTF-16 equivalent of
38+
// U+1D161 is a surrogate pair with hexadecimal values D834 and DD61.
39+
40+
printfn $"{comment}"
41+
let s1 = Char.ConvertFromUtf32 music
42+
printf $" 2a) 0x{music:X} => "
43+
show s1
44+
printfn ""
45+
46+
// Convert the surrogate pair in the string at index position
47+
// zero to a code point.
48+
49+
printfn $"{comment2b}"
50+
let music = Char.ConvertToUtf32(s1, 0)
51+
printf " 2b) "
52+
show s1
53+
printfn $" => 0x{music:X}"
54+
55+
// Convert the high and low characters in the surrogate pair into a code point.
56+
57+
printfn $"{comment2c}"
58+
let music = Char.ConvertToUtf32(s1[0], s1[1])
59+
printf " 2c) "
60+
show s1
61+
printfn $" => 0x{music:X}"
62+
63+
0
64+
65+
// This example produces the following results:
66+
//
67+
// Create a UTF-16 encoded string from a code point.
68+
// 1a) 0x41 => 0x41
69+
// Create a code point from a UTF-16 encoded string.
70+
// 1b) 0x41 => 0x41
71+
//
72+
// Create a UTF-16 encoded string from a code point.
73+
// 2a) 0x1D161 => 0xD834, 0xDD61
74+
// Create a code point from a surrogate pair at a certain position in a string.
75+
// 2b) 0xD834, 0xDD61 => 0x1D161
76+
// Create a code point from a high surrogate and a low surrogate code point.
77+
// 2c) 0xD834, 0xDD61 => 0x1D161
78+
//</snippet1>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="sur.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//<snippet1>
2+
// This example demonstrates the Char.IsLowSurrogate() method
3+
// IsHighSurrogate() method
4+
// IsSurrogatePair() method
5+
open System
6+
7+
let cHigh = '\uD800'
8+
let cLow = '\uDC00'
9+
let s1 = String [| 'a'; '\uD800'; '\uDC00'; 'z' |]
10+
let divider = String.Concat(Environment.NewLine, String('-', 70), Environment.NewLine)
11+
12+
printfn $"""
13+
Hexadecimal code point of the character, cHigh: {int cHigh:X4}
14+
Hexadecimal code point of the character, cLow: {int cLow:X4}
15+
16+
Characters in string, s1: 'a', high surrogate, low surrogate, 'z'
17+
Hexadecimal code points of the characters in string, s1: """
18+
19+
for i = 0 to s1.Length - 1 do
20+
printfn $"s1[{i}] = {int s1[i]:X4} "
21+
22+
printfn $"""{divider}
23+
Is each of the following characters a high surrogate?
24+
A1) cLow? - {Char.IsHighSurrogate cLow}
25+
A2) cHigh? - {Char.IsHighSurrogate cHigh}
26+
A3) s1[0]? - {Char.IsHighSurrogate(s1, 0)}
27+
A4) s1[1]? - {Char.IsHighSurrogate(s1, 1)}
28+
{divider}"""
29+
30+
printfn $"""Is each of the following characters a low surrogate?
31+
B1) cLow? - {Char.IsLowSurrogate cLow}
32+
B2) cHigh? - {Char.IsLowSurrogate cHigh}
33+
B3) s1[0]? - {Char.IsLowSurrogate(s1, 0)}
34+
B4) s1[2]? - {Char.IsLowSurrogate(s1, 2)}
35+
{divider}"""
36+
37+
printfn $"""Is each of the following pairs of characters a surrogate pair?
38+
C1) cHigh and cLow? - {Char.IsSurrogatePair(cHigh, cLow)}
39+
C2) s1[0] and s1[1]? - {Char.IsSurrogatePair(s1, 0)}
40+
C3) s1[1] and s1[2]? - {Char.IsSurrogatePair(s1, 1)}
41+
C4) s1[2] and s1[3]? - {Char.IsSurrogatePair(s1, 2)}"
42+
{divider}"""
43+
44+
// This example produces the following results:
45+
//
46+
// Hexadecimal code point of the character, cHigh: D800
47+
// Hexadecimal code point of the character, cLow: DC00
48+
//
49+
// Characters in string, s1: 'a', high surrogate, low surrogate, 'z'
50+
// Hexadecimal code points of the characters in string, s1:
51+
// s1[0] = 0061
52+
// s1[1] = D800
53+
// s1[2] = DC00
54+
// s1[3] = 007A
55+
//
56+
// ----------------------------------------------------------------------
57+
//
58+
// Is each of the following characters a high surrogate?
59+
// A1) cLow? - False
60+
// A2) cHigh? - True
61+
// A3) s1[0]? - False
62+
// A4) s1[1]? - True
63+
//
64+
// ----------------------------------------------------------------------
65+
//
66+
// Is each of the following characters a low surrogate?
67+
// B1) cLow? - True
68+
// B2) cHigh? - False
69+
// B3) s1[0]? - False
70+
// B4) s1[2]? - True
71+
//
72+
// ----------------------------------------------------------------------
73+
//
74+
// Is each of the following pairs of characters a surrogate pair?
75+
// C1) cHigh and cLow? - True
76+
// C2) s1[0] and s1[1]? - False
77+
// C3) s1[1] and s1[2]? - True
78+
// C4) s1[2] and s1[3]? - False
79+
//
80+
// ----------------------------------------------------------------------
81+
//</snippet1>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// <snippet19>
2+
let chA = 'A'
3+
let chB = 'B'
4+
5+
printfn $"{chA.CompareTo 'A'}" // Output: "0" (meaning they're equal)
6+
printfn $"{'b'.CompareTo chB}" // Output: "32" (meaning 'b' is greater than 'B' by 32)
7+
printfn $"{chA.CompareTo chB}" // Output: "-1" (meaning 'A' is less than 'B' by 1)
8+
9+
// </snippet19>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="compareto.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// <snippet20>
2+
let chA = 'A'
3+
let chB = 'B'
4+
5+
printfn $"{chA.Equals 'A'}" // Output: "True"
6+
printfn $"{'b'.Equals chB}" // Output: "False"
7+
8+
// </snippet20>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="equals.fs" />
8+
</ItemGroup>
9+
</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+
<ItemGroup>
7+
<Compile Include="getnumericvalue1.fs" />
8+
<Compile Include="getnumericvalue.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module getnumericvalue1
2+
3+
// <snippet1>
4+
open System
5+
6+
let str = "input: 1"
7+
8+
printfn $"{Char.GetNumericValue '8'}" // Output: "8"
9+
printfn $"{Char.GetNumericValue(str, 7)}" // Output: "1"
10+
11+
// </snippet1>

0 commit comments

Comments
 (0)