Skip to content
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,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="tp.fs" />
</ItemGroup>
</Project>
133 changes: 133 additions & 0 deletions samples/snippets/fsharp/VS_Snippets_CLR/T.TryParse/FS/tp.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
//<snippet1>
// This example demonstrates overloads of the TryParse method for
// several base types, and the TryParseExact method for DateTime.

// In most cases, this example uses the most complex overload that is, the overload
// with the most parameters for a particular type. If a complex overload specifies
// null (Nothing in Visual Basic) for the IFormatProvider parameter, formatting
// information is obtained from the culture associated with the current thread.
// If a complex overload specifies the style parameter, the parameter value is
// the default value used by the equivalent simple overload.

open System
open System.Globalization

let show parseSuccess typeName parseValue =
if parseSuccess then
printfn $"Parse for %s{typeName} = %s{parseValue}"
else
printfn $"** Parse for %s{typeName} failed. Invalid input."

[<EntryPoint>]
let main _ =
printfn "This example demonstrates overloads of the TryParse method for\nseveral base types, as well as the TryParseExact method for DateTime.\n"

// Non-numeric types:
printfn "Non-numeric types:\n"
// DateTime
// TryParse:
// Assume current culture is en-US, and dates of the form: MMDDYYYY.
let success, datetimeVal = DateTime.TryParse "7/4/2004 12:34:56"
show success "DateTime #1" (string datetimeVal)

// Use fr-FR culture, and dates of the form: DDMMYYYY.
let ci = CultureInfo "fr-FR"
let success, datetimeVal = DateTime.TryParse("4/7/2004 12:34:56", ci, DateTimeStyles.None)
show success "DateTime #2" (string datetimeVal)

// TryParseExact:
// Use fr-FR culture. The format, "G", is short date and long time.
let success, datetimeVal = DateTime.TryParseExact("04/07/2004 12:34:56", "G", ci, DateTimeStyles.None)
show success "DateTime #3" (string datetimeVal)

// Assume en-US culture.
let dateFormats = [| "f"; "F"; "g"; "G" |]
let success, datetimeVal = DateTime.TryParseExact("7/4/2004 12:34:56 PM", dateFormats, null, DateTimeStyles.None)
show success "DateTime #4" (string datetimeVal)

printfn ""
// Boolean
let success, booleanVal = Boolean.TryParse "true"
show success "Boolean" (string booleanVal)
// Char
let success, charVal = Char.TryParse "A"
show success "Char" (string charVal)

// Numeric types:
printfn "\nNumeric types:\n"
// Byte
let success, byteVal = Byte.TryParse("1", NumberStyles.Integer, null)
show success "Byte" (string byteVal)
// Int16
let success, int16Val = Int16.TryParse("-2", NumberStyles.Integer, null)
show success "Int16" (string int16Val)
// Int32
let success, int32Val = Int32.TryParse("3", NumberStyles.Integer, null)
show success "Int32" (string int32Val)
// Int64
let success, int64Val = Int64.TryParse("4", NumberStyles.Integer, null)
show success "Int64" (string int64Val)
// Decimal
let success, decimalVal = Decimal.TryParse("-5.5", NumberStyles.Number, null)
show success "Decimal" (string decimalVal)
// Single
let success, singleVal = Single.TryParse("6.6", NumberStyles.Float ||| NumberStyles.AllowThousands, null)
show success "Single" (string singleVal)
// Double
let success, doubleVal = Double.TryParse("-7", NumberStyles.Float ||| NumberStyles.AllowThousands, null)
show success "Double" (string doubleVal)

// Use the simple Double.TryParse overload, but specify an invalid value.

let success, doubleVal = Double.TryParse "abc"
show success "Double #2" (string doubleVal)
//
printfn "\nThe following types are not CLS-compliant:\n"
// SByte
let success, sbyteVal = SByte.TryParse("-8", NumberStyles.Integer, null)
show success "SByte" (string sbyteVal)
// UInt16
let success, uint16Val = UInt16.TryParse("9", NumberStyles.Integer, null)
show success "UInt16" (string uint16Val)
// UInt32
let success, uint32Val = UInt32.TryParse("10", NumberStyles.Integer, null)
show success "UInt32" (string uint32Val)
// UInt64
let success, uint64Val = UInt64.TryParse("11", NumberStyles.Integer, null)
show success "UInt64" (string uint64Val)

0

// This example produces the following results:
//
// This example demonstrates overloads of the TryParse method for
// several base types, as well as the TryParseExact method for DateTime.
//
// Non-numeric types:
//
// Parse for DateTime #1 = 7/4/2004 12:34:56
// Parse for DateTime #2 = 7/4/2004 12:34:56
// Parse for DateTime #3 = 7/4/2004 12:34:56
// Parse for DateTime #4 = 7/4/2004 12:34:56
//
// Parse for Boolean = True
// Parse for Char = A
//
// Numeric types:
//
// Parse for Byte = 1
// Parse for Int16 = -2
// Parse for Int32 = 3
// Parse for Int64 = 4
// Parse for Decimal = -5.5
// Parse for Single = 6.6
// Parse for Double = -7
// ** Parse for Double #2 failed. Invalid input.
//
// The following types are not CLS-compliant:
//
// Parse for SByte = -8
// Parse for UInt16 = 9
// Parse for UInt32 = 10
// Parse for UInt64 = 11
//</snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="utf.fs" />
</ItemGroup>
</Project>
78 changes: 78 additions & 0 deletions samples/snippets/fsharp/VS_Snippets_CLR/char.cvtutf32/FS/utf.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//<snippet1>
open System

let show (s: string) =
for x = 0 to s.Length - 1 do
printf $"""0x{int s[x]:X}{if x = s.Length - 1 then String.Empty else ", "}"""

[<EntryPoint>]
let main _ =
let letterA = 0x0041 //U+00041 = LATIN CAPITAL LETTER A
let music = 0x1D161 //U+1D161 = MUSICAL SYMBOL SIXTEENTH NOTE
let comment = "Create a UTF-16 encoded string from a code point."
let comment1b = "Create a code point from a UTF-16 encoded string."
let comment2b = "Create a code point from a surrogate pair at a certain position in a string."
let comment2c = "Create a code point from a high surrogate and a low surrogate code point."

// Convert code point U+0041 to UTF-16. The UTF-16 equivalent of
// U+0041 is a Char with hexadecimal value 0041.

printfn $"{comment}"
let s1 = Char.ConvertFromUtf32 letterA
printf $" 1a) 0x{letterA:X} => "
show s1
printfn ""

// Convert the lone UTF-16 character to a code point.

printfn $"{comment1b}"
let letterA = Char.ConvertToUtf32(s1, 0)
printf " 1b) "
show s1
printfn $" => 0x{letterA:X}"
printfn ""

// -------------------------------------------------------------------

// Convert the code point U+1D161 to UTF-16. The UTF-16 equivalent of
// U+1D161 is a surrogate pair with hexadecimal values D834 and DD61.

printfn $"{comment}"
let s1 = Char.ConvertFromUtf32 music
printf $" 2a) 0x{music:X} => "
show s1
printfn ""

// Convert the surrogate pair in the string at index position
// zero to a code point.

printfn $"{comment2b}"
let music = Char.ConvertToUtf32(s1, 0)
printf " 2b) "
show s1
printfn $" => 0x{music:X}"

// Convert the high and low characters in the surrogate pair into a code point.

printfn $"{comment2c}"
let music = Char.ConvertToUtf32(s1[0], s1[1])
printf " 2c) "
show s1
printfn $" => 0x{music:X}"

0

// This example produces the following results:
//
// Create a UTF-16 encoded string from a code point.
// 1a) 0x41 => 0x41
// Create a code point from a UTF-16 encoded string.
// 1b) 0x41 => 0x41
//
// Create a UTF-16 encoded string from a code point.
// 2a) 0x1D161 => 0xD834, 0xDD61
// Create a code point from a surrogate pair at a certain position in a string.
// 2b) 0xD834, 0xDD61 => 0x1D161
// Create a code point from a high surrogate and a low surrogate code point.
// 2c) 0xD834, 0xDD61 => 0x1D161
//</snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="sur.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//<snippet1>
// This example demonstrates the Char.IsLowSurrogate() method
// IsHighSurrogate() method
// IsSurrogatePair() method
open System

let cHigh = '\uD800'
let cLow = '\uDC00'
let s1 = String [| 'a'; '\uD800'; '\uDC00'; 'z' |]
let divider = String.Concat(Environment.NewLine, String('-', 70), Environment.NewLine)

printfn $"""
Hexadecimal code point of the character, cHigh: {int cHigh:X4}
Hexadecimal code point of the character, cLow: {int cLow:X4}

Characters in string, s1: 'a', high surrogate, low surrogate, 'z'
Hexadecimal code points of the characters in string, s1: """

for i = 0 to s1.Length - 1 do
printfn $"s1[{i}] = {int s1[i]:X4} "

printfn $"""{divider}
Is each of the following characters a high surrogate?
A1) cLow? - {Char.IsHighSurrogate cLow}
A2) cHigh? - {Char.IsHighSurrogate cHigh}
A3) s1[0]? - {Char.IsHighSurrogate(s1, 0)}
A4) s1[1]? - {Char.IsHighSurrogate(s1, 1)}
{divider}"""

printfn $"""Is each of the following characters a low surrogate?
B1) cLow? - {Char.IsLowSurrogate cLow}
B2) cHigh? - {Char.IsLowSurrogate cHigh}
B3) s1[0]? - {Char.IsLowSurrogate(s1, 0)}
B4) s1[2]? - {Char.IsLowSurrogate(s1, 2)}
{divider}"""

printfn $"""Is each of the following pairs of characters a surrogate pair?
C1) cHigh and cLow? - {Char.IsSurrogatePair(cHigh, cLow)}
C2) s1[0] and s1[1]? - {Char.IsSurrogatePair(s1, 0)}
C3) s1[1] and s1[2]? - {Char.IsSurrogatePair(s1, 1)}
C4) s1[2] and s1[3]? - {Char.IsSurrogatePair(s1, 2)}"
{divider}"""

// This example produces the following results:
//
// Hexadecimal code point of the character, cHigh: D800
// Hexadecimal code point of the character, cLow: DC00
//
// Characters in string, s1: 'a', high surrogate, low surrogate, 'z'
// Hexadecimal code points of the characters in string, s1:
// s1[0] = 0061
// s1[1] = D800
// s1[2] = DC00
// s1[3] = 007A
//
// ----------------------------------------------------------------------
//
// Is each of the following characters a high surrogate?
// A1) cLow? - False
// A2) cHigh? - True
// A3) s1[0]? - False
// A4) s1[1]? - True
//
// ----------------------------------------------------------------------
//
// Is each of the following characters a low surrogate?
// B1) cLow? - True
// B2) cHigh? - False
// B3) s1[0]? - False
// B4) s1[2]? - True
//
// ----------------------------------------------------------------------
//
// Is each of the following pairs of characters a surrogate pair?
// C1) cHigh and cLow? - True
// C2) s1[0] and s1[1]? - False
// C3) s1[1] and s1[2]? - True
// C4) s1[2] and s1[3]? - False
//
// ----------------------------------------------------------------------
//</snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// <snippet19>
let chA = 'A'
let chB = 'B'

printfn $"{chA.CompareTo 'A'}" // Output: "0" (meaning they're equal)
printfn $"{'b'.CompareTo chB}" // Output: "32" (meaning 'b' is greater than 'B' by 32)
printfn $"{chA.CompareTo chB}" // Output: "-1" (meaning 'A' is less than 'B' by 1)

// </snippet19>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="compareto.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// <snippet20>
let chA = 'A'
let chB = 'B'

printfn $"{chA.Equals 'A'}" // Output: "True"
printfn $"{'b'.Equals chB}" // Output: "False"

// </snippet20>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="equals.fs" />
</ItemGroup>
</Project>
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="getnumericvalue1.fs" />
<Compile Include="getnumericvalue.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module getnumericvalue1

// <snippet1>
open System

let str = "input: 1"

printfn $"{Char.GetNumericValue '8'}" // Output: "8"
printfn $"{Char.GetNumericValue(str, 7)}" // Output: "1"

// </snippet1>
Loading